Skip to content

Commit

Permalink
Merge pull request #55 from suzukiplan/1.9.2
Browse files Browse the repository at this point in the history
Version 1.9.2
  • Loading branch information
suzukiplan authored Jan 20, 2023
2 parents ebade12 + 36d346e commit 32dc731
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## Version 1.9.2 (Jan 20, 2023 JST)

- The value of the upper 8 bits of the port number of the immediate I/O operands (`IN A, (n)` and `OUT (n), A`) when executed in 16-bit port mode has been changed from register B to A.

## Version 1.9.1 (Jan 7, 2023 JST)

- Corrected a degrade in version 1.9.0
Expand Down
14 changes: 8 additions & 6 deletions test/test-out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ int main()
0x3E, 0x01, // LD A, $01
0xED, 0x79, // OUT (C), A
0xED, 0x78, // IN A, (C)
0x3E, 0x56, // LD A, $56
0xDB, 0x78, // OUT (0x78), A
0x3E, 0x9A, // LD A, $9A
0xDB, 0xBC, // IN A, (0xBC)
0xc3, 0x09, 0x00, // JMP $0009
};

Expand All @@ -17,20 +21,18 @@ int main()
}, [](void* arg, unsigned char addr, unsigned char value) {
// nothing to do
}, [](void* arg, unsigned short port) {
printf("IN port A <- $%02X%02X\n",
((Z80*)arg)->reg.pair.B, // the top half (A8 through A15) of the address bus (reg.B)
printf("IN port A <- $%04X\n",
port // the bottom half (A0 through A7) of the address bus to select the I/O device at one of 256 possible ports
);
return 0x00;
}, [](void* arg, unsigned short port, unsigned char value) {
printf("OUT port $%02X%02X <- $%02X\n",
((Z80*)arg)->reg.pair.B, // the top half (A8 through A15) of the address bus (reg.B)
printf("OUT port $%02X <- $%04X\n",
port, // the bottom half (A0 through A7) of the address bus to select the I/O device at one of 256 possible ports
value
);
}, &z80, false);
z80.setDebugMessage([](void* arg, const char* msg) { puts(msg); });
z80.execute(50);
z80.execute(80);
}

{
Expand All @@ -51,7 +53,7 @@ int main()
);
}, &z80, true);
z80.setDebugMessage([](void* arg, const char* msg) { puts(msg); });
z80.execute(50);
z80.execute(80);
}
return 0;
}
20 changes: 16 additions & 4 deletions test/test-out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
[0000] LD BC<$0000>, $1234
[0003] LD A<$FF>, $01
[0005] OUT (C<$34>), A<$01>
OUT port $1234 <- $01
IN port A <- $1234
OUT port $34 <- $0001
IN port A <- $0034
[0007] IN A<$01>, (C<$34>) = $00
[0009] JP $0009
[0009] LD A<$00>, $56
IN port A <- $0078
[000B] IN A<$56>, ($78) = $00
[000D] LD A<$00>, $9A
IN port A <- $00BC
[000F] IN A<$9A>, ($BC) = $00
[0011] JP $0009
=== 16bit port mode ===
[0000] LD BC<$0000>, $1234
[0003] LD A<$FF>, $01
[0005] OUT (C<$34>), A<$01>
OUT port $1234 <- $01
IN port A <- $1234
[0007] IN A<$01>, (C<$34>) = $00
[0009] JP $0009
[0009] LD A<$00>, $56
IN port A <- $5678
[000B] IN A<$56>, ($78) = $00
[000D] LD A<$00>, $9A
IN port A <- $9ABC
[000F] IN A<$9A>, ($BC) = $00
[0011] JP $0009
38 changes: 26 additions & 12 deletions z80.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,32 @@ class Z80
return hz;
}

inline unsigned short getPort16(unsigned char c) { return make16BitsFromLE(c, reg.pair.B); }
inline unsigned short getPort16WithB(unsigned char c) { return make16BitsFromLE(c, reg.pair.B); }
inline unsigned short getPort16WithA(unsigned char c) { return make16BitsFromLE(c, reg.pair.A); }

inline unsigned char inPort(unsigned char port, int clock = 4)
inline unsigned char inPortWithB(unsigned char port, int clock = 4)
{
unsigned char byte = CB.in(CB.arg, CB.returnPortAs16Bits ? getPort16(port) : port);
unsigned char byte = CB.in(CB.arg, CB.returnPortAs16Bits ? getPort16WithB(port) : port);
consumeClock(clock);
return byte;
}

inline void outPort(unsigned char port, unsigned char value, int clock = 4)
inline unsigned char inPortWithA(unsigned char port, int clock = 4)
{
CB.out(CB.arg, CB.returnPortAs16Bits ? getPort16(port) : port, value);
unsigned char byte = CB.in(CB.arg, CB.returnPortAs16Bits ? getPort16WithA(port) : port);
consumeClock(clock);
return byte;
}

inline void outPortWithB(unsigned char port, unsigned char value, int clock = 4)
{
CB.out(CB.arg, CB.returnPortAs16Bits ? getPort16WithB(port) : port, value);
consumeClock(clock);
}

inline void outPortWithA(unsigned char port, unsigned char value, int clock = 4)
{
CB.out(CB.arg, CB.returnPortAs16Bits ? getPort16WithA(port) : port, value);
consumeClock(clock);
}

Expand Down Expand Up @@ -4826,7 +4840,7 @@ class Z80
static inline void IN_A_N(Z80* ctx)
{
unsigned char n = ctx->fetch(3);
unsigned char i = ctx->inPort(n);
unsigned char i = ctx->inPortWithA(n);
if (ctx->isDebug()) ctx->log("[%04X] IN %s, ($%02X) = $%02X", ctx->reg.PC - 2, ctx->registerDump(0b111), n, i);
ctx->reg.pair.A = i;
}
Expand All @@ -4843,7 +4857,7 @@ class Z80
inline void IN_R_C(unsigned char r, bool setRegister = true)
{
unsigned char* rp = setRegister ? getRegisterPointer(r) : nullptr;
unsigned char i = inPort(reg.pair.C);
unsigned char i = inPortWithB(reg.pair.C);
if (rp) {
if (isDebug()) log("[%04X] IN %s, (%s) = $%02X", reg.PC - 2, registerDump(r), registerDump(0b001), i);
*rp = i;
Expand Down Expand Up @@ -4875,7 +4889,7 @@ class Z80
inline void repeatIN(bool isIncHL, bool isRepeat)
{
reg.WZ = (unsigned short)(getBC() + (isIncHL ? 1 : -1));
unsigned char i = inPort(reg.pair.C);
unsigned char i = inPortWithB(reg.pair.C);
decrementB_forRepeatIO();
unsigned short hl = getHL();
if (isDebug()) {
Expand Down Expand Up @@ -4908,7 +4922,7 @@ class Z80
{
unsigned char n = ctx->fetch(3);
if (ctx->isDebug()) ctx->log("[%04X] OUT ($%02X), %s", ctx->reg.PC - 2, n, ctx->registerDump(0b111));
ctx->outPort(n, ctx->reg.pair.A);
ctx->outPortWithA(n, ctx->reg.pair.A);
}

// Output a byte to device (C) form register.
Expand All @@ -4924,10 +4938,10 @@ class Z80
{
if (zero) {
if (isDebug()) log("[%04X] OUT (%s), 0", reg.PC - 2, registerDump(0b001));
outPort(reg.pair.C, 0);
outPortWithB(reg.pair.C, 0);
} else {
if (isDebug()) log("[%04X] OUT (%s), %s", reg.PC - 2, registerDump(0b001), registerDump(r));
outPort(reg.pair.C, getRegister(r));
outPortWithB(reg.pair.C, getRegister(r));
}
}

Expand All @@ -4943,7 +4957,7 @@ class Z80
}
}
decrementB_forRepeatIO();
outPort(reg.pair.C, o);
outPortWithB(reg.pair.C, o);
reg.WZ = (unsigned short)(getBC() + (isIncHL ? 1 : -1));
setHL((unsigned short)(getHL() + (isIncHL ? 1 : -1)));
setFlagZ(reg.pair.B == 0);
Expand Down

0 comments on commit 32dc731

Please sign in to comment.