-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.go
More file actions
69 lines (58 loc) · 1.18 KB
/
function.go
File metadata and controls
69 lines (58 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"os"
"github.com/DanielleB-R/goto6502/lib/cpu"
)
type Registers struct {
A byte
X byte
Y byte
}
func (r *Registers) Matches(processor *cpu.Processor) bool {
return (r.A == processor.A) && (r.X == processor.X) && (r.Y == processor.Y)
}
type MemoryMatch struct {
base int
data []byte
name string
}
func (m *MemoryMatch) Matches(processor *cpu.Processor) bool {
for offset, n := range m.data {
if v := processor.Memory.Read(m.base + offset); v != n {
fmt.Printf("Failure in %s, %x should be %x\n", m.name, v, n)
return false
}
}
return true
}
type Program struct {
MachineCodeFile string
Description string
FinalState Registers
FinalMemory []MemoryMatch
}
func (p *Program) Check() bool {
infile, err := os.Open(p.MachineCodeFile)
if err != nil {
panic(err)
}
defer infile.Close()
cpu := cpu.NewProcessor(0x1000, infile)
for cpu.Memory.Read(cpu.PC) != 0 {
err := cpu.Emulate()
if err != nil {
panic(err)
}
}
for _, mem := range p.FinalMemory {
if !mem.Matches(&cpu) {
return false
}
}
ok := p.FinalState.Matches(&cpu)
if !ok {
fmt.Printf("A %02x X %02x Y %02x\n", cpu.A, cpu.X, cpu.Y)
}
return ok
}