# HG changeset patch # User telesto # Date 1423074592 -3600 # Wed Feb 04 19:29:52 2015 +0100 # Node ID 85c6f1a541e329a769d4831aaff96da7ddc03bb3 # Parent 1ffadbd88f1d78ea766d913667db1188349f987c first tests of timer; fixed test suite; made CPU.setSign() branchless diff --git a/emulate/computer.go b/emulate/computer.go --- a/emulate/computer.go +++ b/emulate/computer.go @@ -7,7 +7,10 @@ const stackLimit = 0x0100 // bottom of stack, starts at 0x01FF -const AtariClockrate = 1790000 +const ( + AtariClockrate = 1789790 + Ultrafast = 1 << 60 +) const ( carry = 1 << iota @@ -33,6 +36,10 @@ return CPU{sp: 0xFF, p: alwaysSet} } +func (cpu *CPU) reset() { + cpu.p = alwaysSet +} + func (cpu *CPU) A() uint8 { return cpu.a } @@ -74,10 +81,7 @@ } func (cpu *CPU) setSign(value uint8) *CPU { - cpu.p &^= sign - if value>>7 == 1 { - cpu.p |= sign - } + cpu.p = cpu.p&^sign | value&0x80 return cpu } @@ -98,6 +102,7 @@ func (c *Computer) Warmstart(pc uint16) { c.clock.reset() + c.cpu.reset() c.cpu.pc = pc return } @@ -110,11 +115,12 @@ func (c *Computer) Step() error { instr := instructionset[c.memory[c.cpu.pc]] - c.cpu.pc++ if !instr.legal { return fmt.Errorf("%d: op code is illegal", instr) } + c.cpu.pc++ instr.op(&c.cpu, c.memory) + c.clock.wait(2) return nil } diff --git a/emulate/instruction_test.go b/emulate/instruction_test.go --- a/emulate/instruction_test.go +++ b/emulate/instruction_test.go @@ -9,14 +9,17 @@ acc uint8 p uint8 }{ - {[]byte{ORA_IMM, 0x10}, 0x10, 0x00}, - {[]byte{ORA_IMM, 0x00}, 0x00, zero}, - {[]byte{ORA_ABS, 0x03, 0x00, 0xF3}, 0xF3, sign}, + {[]byte{ORA_IMM, 0x10}, 0x10, alwaysSet}, + {[]byte{ORA_IMM, 0x00}, 0x00, alwaysSet | zero}, + {[]byte{ORA_ABS, 0x03, 0x00, 0xF3}, 0xF3, alwaysSet | sign}, } func TestInstructions(t *testing.T) { for i, tc := range instructionTests { - c := &Computer{memory: tc.mem} + c := NewComputer(63 << 1) + if err := c.Load(tc.mem, 0); err != nil { + panic(err) + } c.Step() { found := c.cpu.A() @@ -37,7 +40,7 @@ } } -func BenchmarkStep(b *testing.B) { +func BenchmarkFastMultiplyWith10(b *testing.B) { const start = 0x0000 const tmpl, tmph = 12, 00 // fast multiply with 10, from http://6502.org/source/integers/fastx10.htm @@ -50,7 +53,7 @@ CLC, ADC_ABS, tmpl, tmph, 0x00} // tmp variable - comp := NewComputer(AtariClockrate) + comp := NewComputer(Ultrafast) if err := comp.Load(prog, start); err != nil { panic(err) }