Post Reply 
NUT CPU BCD arithmetics in SETDEC mode (aka Goose hunt)
04-24-2024, 05:37 PM
Post: #7
RE: NUT CPU BCD arithmetics in SETDEC mode (aka Goose hunt)
(04-04-2024 07:31 AM)ThomasF Wrote:  Using the following code, I could verify with all examples in previous traces!

Code:
byte Subtractor(byte nib1, byte nib2)
{
  char result=nib1-nib2-CARRY;
  BASECY=0;
  if (result<0) {
    result+=BASE;
    CARRY=1;
  } else {
    CARRY=0;
    if (result>=BASE) {
      result-=(16-10);
      BASECY=1;
    }
  }
  return(result&0x0f);
}

void bcd_sub(char *p1, char *p2, int n)
{
  CARRY=0;
  for (int i=0;i<n;i++) {
    p1[i] = Subtractor(p1[i],p2[i]);
  }
  CARRY|=BASECY;
}

On my request Thomas made an additional test with the ?A<C and ?A<B opcodes.

Like on other CPU's the compare function base on a subtraction without saving the result. So ?A<C base on the subtraction of A-C and has the same side effects on the Carry Flag like A=A-C.

So the less function for the NUT CPU could be implemented as:

Code:
void bcd_less(char *p1, char *p2, int n)
{
  CARRY=0;
  for (int i=0;i<n;i++) {
    Subtractor(p1[i],p2[i]);
  }
  CARRY|=BASECY;
}

The complete source code of V41 R9L with a reference implementation is now available.

Christoph
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: NUT CPU BCD arithmetics in SETDEC mode (aka Goose hunt) - Christoph Giesselink - 04-24-2024 05:37 PM



User(s) browsing this thread: 2 Guest(s)