Skip to content

Commit

Permalink
Merge pull request #13 from koron-go/fix-refresh-counter
Browse files Browse the repository at this point in the history
Fix refresh counter
  • Loading branch information
koron authored Apr 20, 2024
2 parents dd0668b + 232f6e5 commit 133020c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 29 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ $ make zexdoc
* [MSX JAPAN/ファイル形式](https://msxjpn.jimdofree.com/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%BD%A2%E5%BC%8F/)

* [Yet Another Z80 Emulator by AG](http://www.mathematik.uni-ulm.de/users/ag/yaze-ag/)

* [8ビット CPU Z80タイミング](http://www.yamamo10.jp/yamamoto/comp/Z80/Z80_Timming/index.php)

* [Z80のRレジスタについて](https://electrelic.com/electrelic/node/1506)

* [Visual Z80 Remix](https://floooh.github.io/visualz80remix/)
10 changes: 8 additions & 2 deletions cmd/zexdoc/zexdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func runZexdoc(name string) error {
IO: io,
}

var stopProf = func() {}
if cpuprof != "" {
f, err := os.Create(cpuprof)
if err != nil {
Expand All @@ -58,7 +59,9 @@ func runZexdoc(name string) error {
if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("could not start CPU profile: %w", err)
}
defer pprof.StopCPUProfile()
stopProf = func() {
pprof.StopCPUProfile()
}
}

for {
Expand All @@ -68,11 +71,14 @@ func runZexdoc(name string) error {
// TODO:
continue
}
stopProf()
return err
}
break
}

stopProf()

if memprof != "" {
f, err := os.Create(memprof)
if err != nil {
Expand All @@ -85,7 +91,7 @@ func runZexdoc(name string) error {
}
}

if cpu.PC != 0xff04 {
if cpu.PC != 0xff03 {
return fmt.Errorf("halted on unexpected PC: %04x", cpu.PC)
}
return nil
Expand Down
13 changes: 10 additions & 3 deletions cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ func (cpu *CPU) fetch16() uint16 {
return (uint16(h) << 8) | uint16(l)
}

// fetchM1 fetches a byte for M1 cycle.
func (cpu *CPU) fetchM1() uint8 {
c := cpu.Memory.Get(cpu.PC)
cpu.PC++
// increment refresh counter
rc := cpu.IR.Lo
cpu.IR.Lo = rc&0x80 | (rc+1)&0x7f
return c
}

func (cpu *CPU) ioIn(addr uint8) uint8 {
if cpu.IO == nil {
return 0
Expand Down Expand Up @@ -180,9 +190,6 @@ func (cpu *CPU) Run(ctx context.Context) error {

// Step executes an instruction.
func (cpu *CPU) Step() {
// increment refresh counter
rc := cpu.IR.Lo
cpu.IR.Lo = rc&0x80 | (rc+1)&0x7f
// try interruptions.
oldPC := cpu.PC
if cpu.tryInterrupt() {
Expand Down
8 changes: 4 additions & 4 deletions op_arith16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func tADChlss(t *testing.T, r tReg, hl, ss uint16, c bool) {
postGPR.AF.Lo = flags
postSPR := preSPR
postSPR.PC = 0x0002
postSPR.IR = Register{Lo: 0x01}
postSPR.IR = Register{Lo: 0x02}

tSteps(t,
fmt.Sprintf("ADC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t", r.Label, hl, ss, c),
fmt.Sprintf("ADC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t)", r.Label, hl, ss, c),
States{GPR: preGPR, SPR: preSPR}, mem, 1,
States{GPR: postGPR, SPR: postSPR}, mem, maskDefault)
}
Expand Down Expand Up @@ -153,10 +153,10 @@ func tSBChlss(t *testing.T, r tReg, hl, ss uint16, c bool) {
postGPR.AF.Lo = flags
postSPR := preSPR
postSPR.PC = 0x0002
postSPR.IR = Register{Lo: 0x01}
postSPR.IR = Register{Lo: 0x02}

tSteps(t,
fmt.Sprintf("SBC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t", r.Label, hl, ss, c),
fmt.Sprintf("SBC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t)", r.Label, hl, ss, c),
States{GPR: preGPR, SPR: preSPR}, mem, 1,
States{GPR: postGPR, SPR: postSPR}, mem, maskDefault)
}
Expand Down
4 changes: 2 additions & 2 deletions op_arith8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func tADDAIXd(t *testing.T, av, dv uint8, ix uint16, mem MapMemory) {
}, mem, 1,
States{
GPR: postGPR,
SPR: SPR{IX: ix, PC: 0x0003, IR: Register{Lo: 0x01}},
SPR: SPR{IX: ix, PC: 0x0003, IR: Register{Lo: 0x02}},
}, mem, maskDefault)
}

Expand Down Expand Up @@ -535,7 +535,7 @@ func tADDAIYd(t *testing.T, av, dv uint8, iy uint16, mem MapMemory) {
}, mem, 1,
States{
GPR: postGPR,
SPR: SPR{IY: iy, PC: 0x0003, IR: Register{Lo: 0x01}},
SPR: SPR{IY: iy, PC: 0x0003, IR: Register{Lo: 0x02}},
}, mem, maskDefault)
}

Expand Down
14 changes: 7 additions & 7 deletions op_bitop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBitop_BITbr(t *testing.T) {
1,
States{
GPR: wantGPR,
SPR: SPR{PC: 2, IR: Register{Lo: 0x01},
SPR: SPR{PC: 2, IR: Register{Lo: 0x02},
IX: 0x1000,
}},
mem.Clone(),
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestBitop_BITbIXd(t *testing.T) {
1,
States{
GPR: GPR{AF: Register{Lo: flag}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03},
IX: 0x1000,
}},
mem.Clone(),
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestBitop_BITbIYd(t *testing.T) {
1,
States{
GPR: GPR{AF: Register{Lo: flag}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03},
IY: 0x4180,
}},
mem.Clone(),
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestBitop_SETbIXd(t *testing.T) {
States{GPR: GPR{}, SPR: SPR{IX: base}},
mem,
States{GPR: GPR{},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01}, IX: base}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03}, IX: base}},
wantMem)
}
})
Expand All @@ -148,7 +148,7 @@ func TestBitop_SETbIYd(t *testing.T) {
States{GPR: GPR{}, SPR: SPR{IY: base}},
mem,
States{GPR: GPR{},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01}, IY: base}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03}, IY: base}},
wantMem)
}
})
Expand All @@ -171,7 +171,7 @@ func TestBitop_RESbIXd(t *testing.T) {
States{GPR: GPR{}, SPR: SPR{IX: base}},
mem,
States{GPR: GPR{},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01}, IX: base}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03}, IX: base}},
wantMem)
}
})
Expand All @@ -194,7 +194,7 @@ func TestBitop_RESbIYd(t *testing.T) {
States{GPR: GPR{}, SPR: SPR{IY: base}},
mem,
States{GPR: GPR{},
SPR: SPR{PC: 4, IR: Register{Lo: 0x01}, IY: base}},
SPR: SPR{PC: 4, IR: Register{Lo: 0x03}, IY: base}},
wantMem)
}
})
Expand Down
2 changes: 2 additions & 0 deletions op_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func oopDAA(cpu *CPU) {
}

func oopHALT(cpu *CPU) {
// HALT does nothing. Since the program counter (PC) also does not advance, rewind it that was advanced by M1 fetch.
cpu.PC--
cpu.HALT = true
}

Expand Down
17 changes: 7 additions & 10 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package z80

// executeOne executes only an op-code.
func (cpu *CPU) executeOne() {
if cpu.HALT {
return
}
switch c0 := cpu.fetch(); c0 {
switch c0 := cpu.fetchM1(); c0 {
case 0x00:
oopNOP(cpu)

Expand Down Expand Up @@ -602,7 +599,7 @@ func (cpu *CPU) executeOne() {
oopCPn(cpu)

case 0xcb:
switch c1 := cpu.fetch(); c1 {
switch c1 := cpu.fetchM1(); c1 {

// RLC r / RLC (HL)
case 0x00:
Expand Down Expand Up @@ -1185,7 +1182,7 @@ func (cpu *CPU) executeOne() {
}

case 0xdd:
switch c1 := cpu.fetch(); c1 {
switch c1 := cpu.fetchM1(); c1 {

// ADD IX, pp
case 0x09:
Expand Down Expand Up @@ -1540,7 +1537,7 @@ func (cpu *CPU) executeOne() {
oopLDSPIX(cpu)

case 0xcb:
switch d, c3 := cpu.fetch2(); c3 {
switch d, c3 := cpu.fetch(), cpu.fetchM1(); c3 {

case 0x06:
oopRLCIXdP(cpu, d)
Expand Down Expand Up @@ -1628,7 +1625,7 @@ func (cpu *CPU) executeOne() {
}

case 0xed:
switch c1 := cpu.fetch(); c1 {
switch c1 := cpu.fetchM1(); c1 {

// IN r, (C)
// FIXME: IN r[6], (C) to apply flags only.
Expand Down Expand Up @@ -1792,7 +1789,7 @@ func (cpu *CPU) executeOne() {
}

case 0xfd:
switch c1 := cpu.fetch(); c1 {
switch c1 := cpu.fetchM1(); c1 {

// ADD IY, pp
case 0x09:
Expand Down Expand Up @@ -2147,7 +2144,7 @@ func (cpu *CPU) executeOne() {
oopLDSPIY(cpu)

case 0xcb:
switch d, c3 := cpu.fetch2(); c3 {
switch d, c3 := cpu.fetch(), cpu.fetchM1(); c3 {

case 0x06:
oopRLCIYdP(cpu, d)
Expand Down
2 changes: 1 addition & 1 deletion z80_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func tRunMinibios(t *testing.T, name, expOut string, breakpoints ...uint16) {
}
break
}
if cpu.PC != 0xff04 {
if cpu.PC != 0xff03 {
t.Errorf("halted on unexpected PC: %04x", cpu.PC)
}
if s := warnbuf.String(); s != "" {
Expand Down

0 comments on commit 133020c

Please sign in to comment.