first tests of timer; fixed test suite; made CPU.setSign() branchless
2 files changed, 21 insertions(+), 12 deletions(-)

M emulate/computer.go
M emulate/instruction_test.go
M emulate/computer.go +12 -6
@@ 7,7 7,10 @@ import (
 
 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 @@ func newCPU() CPU {
 	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) setZero(value uint8) *CP
 }
 
 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 NewComputer(clockrate int64) *Compu
 
 func (c *Computer) Warmstart(pc uint16) {
 	c.clock.reset()
+	c.cpu.reset()
 	c.cpu.pc = pc
 	return
 }

          
@@ 110,11 115,12 @@ func (c *Computer) Run(pc uint16) (err e
 
 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
 }
 

          
M emulate/instruction_test.go +9 -6
@@ 9,14 9,17 @@ var instructionTests = []struct {
 	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 TestInstructions(t *testing.T) {
 	}
 }
 
-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 @@ func BenchmarkStep(b *testing.B) {
 		CLC,
 		ADC_ABS, tmpl, tmph,
 		0x00} // tmp variable
-	comp := NewComputer(AtariClockrate)
+	comp := NewComputer(Ultrafast)
 	if err := comp.Load(prog, start); err != nil {
 		panic(err)
 	}