Sign in to follow this  
Cups

Help with C# implementation of ASM - SBC.

Recommended Posts

So I'm implementing a gameboy CPU in C# to make an emulator. And I need someone good in ASM to check an implementation I make which I have a doubt on.

 

The OPCODE 0x9F translates to instruction SBC (source here http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html) so substract with carry.

 

I just need someone to check my implementation, like @mesvak cause apparently he knows ASM.

 

Right now I have this made :

 

/// 
       /// Substract n + carry flag from A.
       /// 
       /// n
       private static void sbc(byte r1)
       {
           // TODO Double check this implementation
           uint substractResult = (uint)Gameboy.Cpu.Registers.A - r1 - Convert.ToUInt32(Gameboy.Cpu.Registers.F.C);

           // Set zero flag if result is 0.
           Gameboy.Cpu.Registers.F.Z = ((byte)substractResult) == 0;

           // Set half-carry flag if no borrow from bit 4. IM UNSURE ABOUT THIS. This should mean if result is 0b0001_0000, we have a carry, but is it really how to implement it?
           Gameboy.Cpu.Registers.F.H = ((Gameboy.Cpu.Registers.A ^ r1 ^ substractResult) & 0x10) != 0;

           // Set the carry flag if no borrow at all 0b0001_0000_0000 (so more than 8 bits)
           Gameboy.Cpu.Registers.F.C = ((substractResult & 0x100) != 0);
           
           // The substract flag is set as we performed a sub op.
           Gameboy.Cpu.Registers.F.N = true;
       }

Share this post


Link to post
Share on other sites

i never tried it with c# tbh but tried it with java like 2 years ago and about 3 months ago there was a person who created a similar shit

which is this guy https://github.com/trekawek/coffee-gb/tree/master/src/main/java/eu/rekawek/coffeegb/cpu

 

But its written in java i can translate it to c# if u didnt get it

 

just give a quick brief shit so basically for writing an emulator you needa consider 1- cpu ( timing and .....) 2- memory

 

and dont forget you needa write diff script for each of them seemingly in this script that tbh i m a bit confused about what u written there cuase you cannot bind them without importing them

 

APprently this is the case you working on

}

 

public OpcodeBuilder proceedIf(String condition) {

ops.add(new Op() {

@Override

public boolean proceed(Registers registers) {

switch (condition) {

case "NZ":

return !registers.getFlags().isZ();

 

case "Z":

return registers.getFlags().isZ();

 

case "NC":

return !registers.getFlags().isC();

 

case "C":

return registers.getFlags().isC();

}

 

 

And as a reminder you needa mention the the value of bit u going to use and what is z is it below z<8 bits or ......

Share this post


Link to post
Share on other sites

No, I understand everything about the emulator but I just need someone to check the implementation of the half carry flag

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this