-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulate_sync.go
143 lines (106 loc) · 2.74 KB
/
simulate_sync.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package dodosim
import (
"strings"
)
type SimulatorSync struct {
Renderer Renderer
Speaker Speaker
CyclesPerFrame func(cycles uint64)
Bus *Bus
Cpu *Cpu
Gamepad *Gamepad
Resolve *Resolve
Rom *Rom
Fram *Fram
Cycles uint64
LastOp uint8
WaitTester int
WaitingForInterrupt bool
MissedFrames int
}
func (s *SimulatorSync) PumpClock(input string) {
s.Gamepad.A = strings.Contains(input, "A")
s.Gamepad.B = strings.Contains(input, "B")
s.Gamepad.U = strings.Contains(input, "U")
s.Gamepad.D = strings.Contains(input, "D")
s.Gamepad.L = strings.Contains(input, "L")
s.Gamepad.R = strings.Contains(input, "R")
for {
opcode := s.Bus.Read(s.Cpu.PC)
s.Cpu.PC++
s.Cpu.Status |= Constant
o := GetOperation(opcode)
c := o.Cycles
s.Resolve.Opcode = opcode
s.Resolve.Mode = o.Mode
s.Resolve.Resolve()
pop, rc := o.Handler(s.Resolve)
c += rc
if s.Resolve.Penalty && pop {
c += 1
}
s.Cycles += uint64(c)
if (s.LastOp == 0xA5 && opcode == 0xF0) || (s.LastOp == 0xF0 && opcode == 0xA5) {
s.WaitTester++
} else {
s.WaitTester = 0
}
s.LastOp = opcode
// If Lda, Beq sequences happens 5 times in a row then assume we are waiting for interrupt
if s.WaitTester == 10 { // Getting in here tells us that a complete game cycle was performed
s.CyclesPerFrame(uint64(s.MissedFrames*50000) + s.Cycles)
s.MissedFrames = 0
s.Cycles = 0
s.WaitTester = 0
s.Cpu.Irq(s.Bus)
return
} else if s.Cycles >= 50000 { // If we hit 50000 cycles then that means we need to pause for a whole additional interrupt cycle
s.Cycles = 0
s.MissedFrames++
s.Cpu.Irq(s.Bus)
return
}
}
}
func (s *SimulatorSync) SwitchFram(firmware, game []byte) {
for i, b := range firmware {
s.Rom[i] = b
}
s.Fram.New(game, s.Fram.Flusher)
s.Cpu.Reset(s.Bus)
}
func (s *SimulatorSync) SimulateSyncInit(firmware, game []byte, flusher func(f *Fram)) {
s.Bus = new(Bus)
s.Bus.New()
s.Resolve = new(Resolve)
ram := new(Ram)
s.Bus.Add(ram)
s.Rom = new(Rom)
ssd1305 := new(Ssd1305)
ssd1305.New(ram, s.Renderer)
s.Bus.Add(ssd1305)
s.Gamepad = new(Gamepad)
s.Gamepad.New()
s.Fram = new(Fram)
s.Fram.New(game, flusher)
via := new(Via)
via.New(s.Gamepad, s.Fram, s.Speaker)
s.Bus.Add(via)
acia := new(Acia)
s.Bus.Add(acia)
for i, b := range firmware {
s.Rom[i] = b
}
s.Bus.Add(s.Rom)
s.Bus.BuildMap()
s.Cpu = new(Cpu)
s.Cpu.Reset(s.Bus)
s.Resolve.Cpu = s.Cpu
s.Resolve.Space = s.Bus
BuildTable()
s.Cycles = 0
s.LastOp = 0
s.WaitTester = 0
s.WaitingForInterrupt = false
s.MissedFrames = 0
}