Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.2KB

  1. This file describe tricks and conventions that are used throughout the code and
  2. might need explanation.
  3. *** Quickies
  4. or a: Equivalent to "cp 0", but results in a shorter opcode.
  5. xor a: sets A to 0 more efficiently than ld a, 0
  6. and 0xbf: Given a letter in the a-z range, changes it to its uppercase value
  7. if it's already uppercased, then it stays that way.
  8. *** Z flag for results
  9. Z if almost always used as a success indicator for routines. Set for success,
  10. Reset for failure. "xor a" (destroys A) and "cp a" (preserves A) are used to
  11. ensure Z is set. To ensure that it is reset, it's a bit more complicated and
  12. "unsetZ" routine exists for that, although that in certain circumstances,
  13. "inc a \ dec a" or "or a" can work.
  14. *** Little endian
  15. z80 is little endian in its 16-bit loading operations. For example, "ld hl, (0)"
  16. will load the contents of memory address 0 in L and memory address 1 in H. This
  17. little-endianess is followed by Collapse OS in most situations. When it's not,
  18. it's specified in comments.
  19. This get a bit awkward with regards to 32-bit. There are no "native" z80 32-bit
  20. operations, so z80 doesn't mandate an alignment. In Collapse OS, 32-bit numbers
  21. are stored as "big endian pair of little endian 16-bit numbers". For example,
  22. if "ld dehl, (0)" existed and if the first 4 bytes of memory were 0x01, 0x02,
  23. 0x03 and 0x04, then DE (being the "high" word) would be 0x0201 and HL would be
  24. 0x0403.
  25. *** DAA
  26. When it comes to dealing with decimals, the DAA instruction, which look a bit
  27. obscur, can be very useful. It transforms the result of a previous arithmetic
  28. operation involving two BCD (binary coded decimal, one digit in high nibble,
  29. the other digit in low nibble. For example, 0x99 represents 99) into a valid
  30. BCD. For example, 0x12+0x19=0x2b, but after calling DAA, it will be 0x31.
  31. To clear misunderstanding: this does **not** transform an arbitrary value into
  32. BCD. For example, "ld a, 0xff \ daa" isn't going to magically give you a binary
  33. coded 255 (how could it?). This is designed to be ran after an arithmetic
  34. operation.
  35. A common trick to transform an arbitrary number to BCD is to loop 8 times over
  36. your bitstream, SLA your bits out of your binary value and then run
  37. "adc a, a \ daa" over it (with provisions for carries if you expect numbers
  38. over 99).