superzazu/z80

Minor inconsistency in order of reading the bytes of a word

Opened this issue · 0 comments

In C, if you do an expression like

  myvar = myfunc1() + myfunc2();

the order of calling myfunc1() and myfunc2() is undefined by the standard.

There is code like this in the function rw() which reads both bytes of a word in the same expression. For my project I'm tracing the memory accesses for debugging purposes and it caused me some false positive mismatches when I was comparing with a known good trace that reads the LS byte first.

So for my purposes I fixed the issue with the following patch, you may or may not want to adopt it.

diff --git a/z80.c b/z80.c
index 8813fec..fdf04a6 100644
--- a/z80.c
+++ b/z80.c
@@ -56,8 +56,8 @@ static INLINE void wb(z80* const z, uint16_t addr, uint8_t val) {
 }
 
 static INLINE uint16_t rw(z80* const z, uint16_t addr) {
-  return (z->read_byte(z->userdata, addr + 1) << 8) |
-         z->read_byte(z->userdata, addr);
+  uint8_t data = z->read_byte(z->userdata, addr);
+  return (z->read_byte(z->userdata, addr + 1) << 8) | data;
 }
 
 static INLINE void ww(z80* const z, uint16_t addr, uint16_t val) {