-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.asm
350 lines (270 loc) · 5.41 KB
/
Main.asm
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
.incscript "Scripts.cs"
.sdsctag Program.Version, Program.Name, Program.Notes, Program.Author
.defpage 0, kb(16), $0000
.defpage 1, kb(16), $4000
.defpage 2, kb(16), $8000
.defpage 3, kb(16), $8000
.page 0
; Resources
.include "Tokens.inc"
.emptyfill $FF
; BBC BASIC's scratch memory will be in RAM ($DC00..$DEFF)
EndOfVariables = $DC00
Variables = EndOfVariables - 272
HIMEM = Variables
.function allocVar(size)
allocVar = Variables
Variables += size
.endfunction
PAGE = allocVar(2)
IOControl = allocVar(1)
TrapKeyboardCounter = allocVar(1)
TempPtr = allocVar(2)
TempCapacity = allocVar(2)
TempSize = allocVar(2)
.org $00
di
im 1
jp Boot
BankedCallPreserveHL = allocVar(2)
BankedCallPreserveDE = allocVar(2)
.org $08
ld (BankedCallPreserveHL),hl
+: ld (BankedCallPreserveDE),de
pop hl ; Return address.
ld e,(hl)
inc hl
ld d,(hl)
inc hl
push hl ; Return past vector.
ld hl,+
push hl ; We need to return to our RAM-reenabler.
; Disable RAM.
push af
ld a,($FFFC)
and ~(1 << 3)
ld ($FFFC),a
pop af
; call (de)
push de
ld hl,(BankedCallPreserveHL)
ld de,(BankedCallPreserveDE)
ret
+: ; Enable RAM.
push af
ld a,($FFFC)
or 1 << 3
ld ($FFFC),a
pop af
; Return properly.
ret
.org $38
jp Interrupt
.org $66
call Host.PressEscapeKey
retn
FrameCounter = allocVar(1)
Interrupt:
push af
; Check the reset button.
in a,($DD)
bit 4,a
call z,Host.PressBreakKey
in a,($BF)
FrameInterrupt:
; Handle the 100Hz TIME counter
ld a,(FrameCounter)
sub 100 ; 100Hz timer
jr nc,FinishedFrameInterrupt
-: push hl
push af
; Decrement the trap keyboard counter.
ld a,(TrapKeyboardCounter)
dec a
jp m,+
ld (TrapKeyboardCounter),a
+:
; Update TIME.
ld hl,(Host.TIME)
inc hl
ld (Host.TIME),hl
ld a,h
or l
jr nz,+
ld hl,(Host.TIME+2)
inc hl
ld (Host.TIME+2),hl
+:
; Indicate that the host can try to read the keyboard if it wants.
ld a,(Sound.Status)
bit Sound.Status.Active,a
jr z,SoundIsInactive
ld a,(Sound.ChannelUpdateTimer)
cp Sound.ChannelUpdatePeriod
jr nz,SoundIsBusy
SoundIsInactive:
ld a,(Host.Flags)
set Host.GetKeyPending,a
ld (Host.Flags),a
SoundIsBusy:
CheckingKeyboardInInterrupt:
; Update the sound.
call Sound.Tick
ld a,(VDU.FieldRate)
ld l,a
pop af
add a,l
pop hl
jr nc,-
FinishedFrameInterrupt:
ld (FrameCounter),a
pop af
ei
reti
; Libraries:
.include "Video.asm"
.include "AT.asm"
.include "Keyboard.asm"
.include "KeyboardBuffer.asm"
.include "UK.inc"
.include "Serial.asm"
.include "PCLink2.asm"
.include "Maths.asm"
.include "CLI.asm"
.include "Tape.asm"
.include "Sound.asm"
.include "VDrive.asm"
.include "File.asm"
Boot:
; Make sure SP points somewhere sensible.
ld sp,HIMEM
; Set paging to something sensible.
xor a
ld ($FFFC),a
ld ($FFFD),a
inc a
ld ($FFFE),a
inc a
ld ($FFFF),a
; Assume we have a stock 8KB machine, so place PAGE at $C000.
ld hl,$C000
ld (PAGE),hl
; Additional memory is in the range $8000..$BFFF
; Enable the cartridge RAM.
ld a,($FFFC)
or %00001000 ; RAM enable ($8000..$BFFF)
ld ($FFFC),a
; Check if the cartridge RAM is writable.
ld hl,$8000
ld b,0
-: ld a,(hl)
push hl
inc hl
dec hl
pop hl
inc (hl)
push hl
inc hl
dec hl
pop hl
cp (hl)
jp z,NoCartridgeRAM
djnz -
; How much cartridge RAM do we have?
ld de,1024 ; Start with 1KB.
ld b,5 ; Check for 1KB, 2KB, 4KB, 8KB, 16KB.
ld ix,$8000
-: push ix
pop iy
add iy,de
; Check the mirror address - changing (ix) should not change (iy).
ld a,(iy)
inc (ix)
sub (iy)
dec (ix)
or a
jr nz,+
sla d
djnz -
srl d
+:
; DE = amount of cartridge RAM.
; Decrease PAGE backwards to include it in BASIC's memory map.
ld hl,(PAGE)
or a
sbc hl,de
ld (PAGE),hl
jr FoundCartridgeRAM
NoCartridgeRAM:
ld a,($FFFC)
and %11110111 ; RAM disable ($8000..$BFFF)
ld ($FFFC),a
FoundCartridgeRAM:
; Set up the IOControl mirror
ld a,%11111111
ld (IOControl),a
out ($3F),a
; Set the TRAP keyboard counter to a sensible default.
xor a
ld (TrapKeyboardCounter),a
Main:
; Reset the VDP to sensible defaults.
call Video.Reset
; Clear VRAM and palette.
call Video.ClearAll
; Reset the VDU.
.bcall "VDU.Reset"
; Load the keyboard layout.
ld hl,KeyboardLayouts.UK
call Keyboard.LoadManualLayout
; Serial port initialisation.
call Serial.Reset
; Sound initialisation.
call Sound.Reset
; File system reset.
call File.Reset
; Sign on message.
ld hl,SignOnMessage
.bcall "VDU.PutString"
; How much RAM do we have?
ld hl,$E000 ; Top of memory
ld de,(PAGE)
or a
sbc hl,de
srl h
srl h
ld b,h
xor a
-: inc a
djnz -
.bcall "VDU.PutDecimalByte"
ld a,'K'
.bcall "VDU.PutChar"
ld a,'\r'
.bcall "VDU.PutChar"
; Jump into BASIC.
jp Basic.BBCBASIC_START
SignOnMessage:
.db "SEGA Master System ",0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; *TIJUMP,MAIN/P:4100,EXEC,EVAL,FPP,RAM/P:DC00
; *BBCBASIC/N/Y/E
.if $>$4000
.fail "Too much code."
.else
.echoln strformat("{0} bytes free for code page page 0.", $4000 - $)
.endif
.page 1
.include "BBC BASIC.asm"
.include "Host.asm"
PCLink2.Trap = Host.TrapFileTransfers
.echoln strformat("{0} bytes free for code on page 1.", $8000 - $)
.page 2
.include "VDU.asm"
.echoln strformat("{0} bytes free for code on page 2.", $C000 - $)
.page 3
.if Variables > EndOfVariables
.fail strformat("Too many variables! Please free up {0} bytes.", Variables - EndOfVariables)
.else
.echoln strformat("{0} bytes free for variables.", EndOfVariables - Variables)
.endif