0

I'm designing a Z80 compatible project. I'm up to designing the flags register.

I originally thought that the flags were generated straight from the ALU depending on the inputs and type of ALU operation.

But after looking at the instructions and the flags result it doesn't seem that the flags are always consistent with this logic.

As a result I'm then assuming I also have to feed the ALU the op-code as well to generate the correct flags each time. But this would seem to make the design over-complicated. And before making this huge design step I wanted to check with the Internet.

Am I correct? OR just really confused, and it is as simple as I originally thought?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

3

Of course, the type of the operation is important. Consider overflow when doing addition and subtraction. Say, you're adding or subtracting 8-bit bytes:

1+5=6 - no overflow

255+7=6 - overflow

1-5=252 - overflow

200-100=100 - no overflow

200+100=44 - overflow

100-56=44 - no overflow

Clearly, the carry flag's state here depends not only on the input bytes or the resultant byte value, but also on the operation. And it indicates unsigned overflow.

The logic is very consistent. If it's not, it's time to read the documentation to learn the official logic.

You might be interested in this question.

Community
  • 1
  • 1
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

Your code is written for a CP/M operating system. I/O is done through the BDOS (Basic Disc Operating System) interface. Basically, you load an operation code in the C register, any additional parameters in other registers, and call location 0x5. Function code C=2 is write a character in the E register to the console (=screen). You can see this in action at line 1200:

    ld  e,a
    ld  c,2
    call    bdos
    pop hl
    pop de
    pop bc
    pop af
    ret
 bdos   push    af
    push    bc
    push    de
    push    hl
    call    5
    pop hl
    pop de
    pop bc
    pop af
    ret 

For a reference to BDOS calls, try here. To emulate this you need to trap calls to address 5 and implement them using whatever facilities you have available to you.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Max
  • 2,121
  • 3
  • 16
  • 20