-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.go
169 lines (140 loc) · 3.96 KB
/
environment.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"os"
"time"
"github.com/ivanizag/iz6502"
)
type environment struct {
cpu *iz6502.State
mem *acornMemory
vdu *vdu
con console
// clock, used by OSWORD01 and 02
referenceTime time.Time
// timer, used by OSWORD03 and 04
timer uint64 // Only 40 bits are used
lastTimerUpdate time.Time
// files
file [maxFiles]*os.File
// behaviour
stop bool
lastEscapeTimestamp time.Time
// configuration
apiLog bool
apiLogIO bool
panicOnErr bool
}
func newEnvironment(roms []*string, cpuLog bool, apiLog bool, apiLogIO bool, memLog bool, panicOnErr bool) *environment {
var env environment
env.referenceTime = time.Now()
env.timer = 0
env.lastTimerUpdate = time.Now()
env.lastEscapeTimestamp = time.Now()
env.mem = newAcornMemory(memLog)
//env.cpu = iz6502.NewNMOS6502(env.mem)
env.cpu = iz6502.NewCMOS65c02(env.mem)
env.cpu.SetTrace(cpuLog)
env.vdu = newVdu(&env)
env.apiLog = apiLog
env.apiLogIO = apiLogIO
env.panicOnErr = panicOnErr
env.mem.loadFirmware()
for i, rom := range roms {
if *rom != "" {
env.mem.loadRom(*rom, uint8(0xf-i))
}
}
env.mem.completeWithRam()
initOSVars(&env)
return &env
}
func (env *environment) close() {
env.con.close()
}
func (env *environment) escape() {
timestamp := time.Now()
delay := timestamp.Sub(env.lastEscapeTimestamp)
if delay.Milliseconds() < controlCDelayToQuitMs {
// Two control-c in fast succession, quit
env.close()
os.Exit(0)
}
env.lastEscapeTimestamp = timestamp
env.mem.Poke(zpEscapeFlag, 0x80)
}
func (env *environment) initUpperLanguage() {
for slot := 0xf; slot >= 0; slot-- {
romType := env.mem.data[mosRomTypeTable+uint16(slot)]
if romType&0x40 != 0 {
env.initLanguage(uint8(slot))
return
}
}
panic("There is no language ROM available to boot")
}
func (env *environment) initLanguage(slot uint8) {
//See https://github.com/raybellis/mos120/blob/master/mos120.s#L6186
env.mem.Poke(mosCurrentLanguage, slot)
env.mem.Poke(zpROMSelect, slot)
env.mem.Poke(sheilaRomLatch, slot)
/*
Next, the MOS will set the error point at &FD/&FE to point at the version string (or copyright
message if no version string is present).
*/
copyrightAddress := 0x8000 + 1 + uint16(env.mem.Peek(romCopyrightOffsetPointer))
env.mem.pokeWord(zpErrorPointer, copyrightAddress)
/*
The MOS also automatically prints the ROM's title string (&8009) so that the user is acknowledged.
*/
language := env.mem.peekString(romTitleString, 0)
env.con.writef("%s\n\n", language)
_, x, y, p := env.cpu.GetAXYP()
env.cpu.SetAXYP(1, x, y, p)
env.cpu.SetPC(romStartAddress)
}
func (env *environment) raiseError(code uint8, msg string) {
/*
The BBC microcomputer adopts a standard pattern of bytes
following a BRK instruction, this is:
A single byte error number
An error message
A zero byte to terminate the message
TODO: set proper error codes
http://chrisacorns.computinghistory.org.uk/docs/SJR/SJR_HDFSSysMgrManual.pdf
*/
env.storeError(errorArea, code, msg, errorMessageMaxLength)
env.cpu.SetPC(errorArea)
env.log(fmt.Sprintf("RAISE(ERR=%02x, '%s')", code, msg))
}
func (env *environment) storeError(address uint16, code uint8, msg string, maxMsgLen int) {
/*
The BBC microcomputer adopts a standard pattern of bytes
following a BRK instruction, this is:
A single byte error number
An error message
A zero byte to terminate the message
TODO: set proper error codes
http://chrisacorns.computinghistory.org.uk/docs/SJR/SJR_HDFSSysMgrManual.pdf
*/
env.mem.Poke(address, 0x00 /* BRK opcode */)
env.mem.Poke(address+1, code)
env.mem.pokeString(address+2, msg, 0, uint8(maxMsgLen))
}
func (env *environment) log(msg string) {
if env.apiLog {
fmt.Printf("[[[%s]]]\n", msg)
}
}
func (env *environment) logIO(msg string) {
if env.apiLogIO {
fmt.Printf("[[[%s]]]\n", msg)
}
}
func (env *environment) notImplemented(feature string) {
msg := fmt.Sprintf("Not implemented: %s", feature)
if env.panicOnErr {
panic(msg)
}
env.log(msg)
}