Debian Codegraph @

help/figures/pep8os.pep

raw
1 ;******* Pep/8 Operating System, 2004/08/30
2 ;
3 TRUE: .EQUATE 1
4 FALSE: .EQUATE 0
5 ;
6 ;******* Operating system RAM
7 osRAM: .BLOCK 128 ;System stack area
8 wordBuff:.BLOCK 1 ;Input/output buffer
9 byteBuff:.BLOCK 1 ;Least significant byte of wordBuff
10 wordTemp:.BLOCK 1 ;Temporary word storage
11 byteTemp:.BLOCK 1 ;Least significant byte of tempWord
12 addrMask:.BLOCK 2 ;Addressing mode mask
13 opAddr: .BLOCK 2 ;Trap instruction operand address
14 ;
15 ;******* Operating system ROM
16 .BURN 0xFFFF
17 ;
18 ;******* System Loader
19 ;Data must be in the following format:
20 ;Each hex number representing a byte must contain exactly two
21 ;characters. Each character must be in 0..9, A..F, or a..f and
22 ;must be followed by exactly one space. There must be no
23 ;leading spaces at the beginning of a line and no trailing
24 ;spaces at the end of a line. The last two characters in the
25 ;file must be lowercase zz, which is used as the terminating
26 ;sentinel by the loader.
27 ;
28 loader: LDX 0,i ;X := 0
29 STX wordBuff,d ;Clear input buffer word
30 ;
31 getChar: CHARI byteBuff,d ;Get first hex character
32 LDA wordBuff,d ;Put ASCII into low byte of A
33 CPA 'z',i ;If end of file sentinel 'z'
34 BREQ stopLoad ;then exit loader routine
35 CPA '9',i ;If characer <= '9', assume decimal
36 BRLE shift ;and right nybble is correct digit
37 ADDA 9,i ;else convert nybble to correct digit
38 shift: ASLA ;Shift left by four bits to send
39 ASLA ;the digit to the most significant
40 ASLA ;position in the byte
41 ASLA
42 STBYTEA byteTemp,d ;Save the most significant nybble
43 CHARI byteBuff,d ;Get second hex character
44 LDA wordBuff,d ;Put ASCII into low byte of A
45 CPA '9',i ;If characer <= '9', assume decimal
46 BRLE combine ;and right nybble is correct digit
47 ADDA 9,i ;else convert nybble to correct digit
48 combine: ANDA 0x000F,i ;Mask out the left nybble
49 ORA wordTemp,d ;Combine both hex digits in binary
50 STBYTEA 0,x ;Store in Mem[X]
51 ADDX 1,i ;X := X + 1
52 CHARI byteBuff,d ;Skip blank or <LF>
53 BR getChar ;
54 ;
55 stopLoad:STOP ;
56 ;
57 ;******* Trap handler
58 oldIR: .EQUATE 9 ;Stack address of IR on trap
59 ;
60 trap: LDX 0,i ;Clear X for a byte compare
61 LDBYTEX oldIR,s ;X := trapped IR
62 CPX 0x0028,i ;If X >= first nonunary trap opcode
63 BRGE nonUnary ;trap opcode is nonunary
64 ;
65 unary: ANDX 0x0003,i ;Mask out all but rightmost two bits
66 ASLX ;An address is two bytes
67 CALL unaryJT,x ;Call unary trap routine
68 RETTR ;Return from trap
69 ;
70 unaryJT: .ADDRSS opcode24 ;Address of NOP0 subroutine
71 .ADDRSS opcode25 ;Address of NOP1 subroutine
72 .ADDRSS opcode26 ;Address of NOP2 subroutine
73 .ADDRSS opcode27 ;Address of NOP3 subroutine
74 ;
75 nonUnary:ASRX ;Trap opcode is nonunary
76 ASRX ;Discard addressing mode bits
77 ASRX
78 SUBX 5,i ;Adjust so that NOP opcode = 0
79 ASLX ;An address is two bytes
80 CALL nonUnJT,x ;Call nonunary trap routine
81 return: RETTR ;Return from trap
82 ;
83 nonUnJT: .ADDRSS opcode28 ;Address of NOP subroutine
84 .ADDRSS opcode30 ;Address of DECI subroutine
85 .ADDRSS opcode38 ;Address of DECO subroutine
86 .ADDRSS opcode40 ;Address of STRO subroutine
87 ;
88 ;******* Assert valid trap addressing mode
89 oldIR4: .EQUATE 13 ;oldIR + 4 with two return addresses
90 assertAd:LDA 1,i ;A := 1
91 LDBYTEX oldIR4,s ;X := OldIR
92 ANDX 0x0007,i ;Keep only the addressing mode bits
93 BREQ testAd ;000 = immediate addressing
94 loop: ASLA ;Shift the 1 bit left
95 SUBX 1,i ;Subtract from addressing mode count
96 BRNE loop ;Try next addressing mode
97 testAd: ANDA addrMask,d ;AND the 1 bit with legal modes
98 BREQ addrErr
99 RET0 ;Legal addressing mode, return
100 addrErr: CHARO '\n',i
101 LDA trapMsg,i ;Push address of error message
102 STA -2,s
103 SUBSP 2,i ;Call print subroutine
104 CALL prntMsg
105 STOP ;Halt: Fatal runtime error
106 trapMsg: .ASCII "ERROR: Invalid trap addressing mode.\x00"
107 ;
108 ;******* Set address of trap operand
109 oldX4: .EQUATE 7 ;oldX + 4 with two return addresses
110 oldPC4: .EQUATE 9 ;oldPC + 4 with two return addresses
111 oldSP4: .EQUATE 11 ;oldSP + 4 with two return addresses
112 setAddr: LDBYTEX oldIR4,s ;X := old instruction register
113 ANDX 0x0007,i ;Keep only the addressing mode bits
114 ASLX ;An address is two bytes
115 BR addrJT,x
116 addrJT: .ADDRSS addrI ;Immediate addressing
117 .ADDRSS addrD ;Direct addressing
118 .ADDRSS addrN ;Indirect addressing
119 .ADDRSS addrS ;Stack relative addressing
120 .ADDRSS addrSF ;Stack relative deferred addressing
121 .ADDRSS addrX ;Indexed addressing
122 .ADDRSS addrSX ;Stack indexed addressing
123 .ADDRSS addrSXF ;Stack indexed deferred addressing
124 ;
125 addrI: LDX oldPC4,s ;Immediate addressing
126 SUBX 2,i ;Oprnd = OprndsSpec
127 STX opAddr,d
128 RET0
129 ;
130 addrD: LDX oldPC4,s ;Direct addressing
131 SUBX 2,i ;Oprnd = Mem[OprndSpec]
132 LDX 0,x
133 STX opAddr,d
134 RET0
135 ;
136 addrN: LDX oldPC4,s ;Indirect addressing
137 SUBX 2,i ;Oprnd = Mem[Mem[OprndSpec]]
138 LDX 0,x
139 LDX 0,x
140 STX opAddr,d
141 RET0
142 ;
143 addrS: LDX oldPC4,s ;Stack relative addressing
144 SUBX 2,i ;Oprnd = Mem[SP + OprndSpec]
145 LDX 0,x
146 ADDX oldSP4,s
147 STX opAddr,d
148 RET0
149 ;
150 addrSF: LDX oldPC4,s ;Stack relative deferred addressing
151 SUBX 2,i ;Oprnd = Mem[Mem[SP + OprndSpec]]
152 LDX 0,x
153 ADDX oldSP4,s
154 LDX 0,x
155 STX opAddr,d
156 RET0
157 ;
158 addrX: LDX oldPC4,s ;Indexed addressing
159 SUBX 2,i ;Oprnd = Mem[OprndSpec + X]
160 LDX 0,x
161 ADDX oldX4,s
162 STX opAddr,d
163 RET0
164 ;
165 addrSX: LDX oldPC4,s ;Stack indexed addressing
166 SUBX 2,i ;Oprnd = Mem[SP + OprndSpec + X]
167 LDX 0,x
168 ADDX oldX4,s
169 ADDX oldSP4,s
170 STX opAddr,d
171 RET0
172 ;
173 addrSXF: LDX oldPC4,s ;Stack indexed deferred addressing
174 SUBX 2,i ;Oprnd = Mem[Mem[SP + OprndSpec] + X]
175 LDX 0,x
176 ADDX oldSP4,s
177 LDX 0,x
178 ADDX oldX4,s
179 STX opAddr,d
180 RET0
181 ;
182 ;******* Opcode 0x24
183 ;The NOP0 instruction.
184 opcode24:RET0
185 ;
186 ;******* Opcode 0x25
187 ;The NOP1 instruction.
188 opcode25:RET0
189 ;
190 ;******* Opcode 0x26
191 ;The NOP2 instruction.
192 opcode26:RET0
193 ;
194 ;******* Opcode 0x27
195 ;The NOP3 instruction.
196 opcode27:RET0
197 ;
198 ;******* Opcode 0x28
199 ;The NOP instruction.
200 opcode28:LDA 0x0001,i ;Assert i
201 STA addrMask,d
202 CALL assertAd
203 RET0
204 ;
205 ;******* Opcode 0x30
206 ;The DECI instruction.
207 ;Input format: Any number of leading spaces or line feeds are
208 ;allowed, followed by '+', '-' or a digit as the first character,
209 ;after which digits are input until the first nondigit is
210 ;encountered. The status flags N,Z and V are set appropriately
211 ;by this DECI routine. The C status flag is not affected.
212 ;
213 oldNZVC: .EQUATE 14 ;Stack address of NZVC on interrupt
214 ;
215 total: .EQUATE 10 ;Cumulative total of DECI number
216 valAscii:.EQUATE 8 ;Value(asciiCH)
217 isOvfl: .EQUATE 6 ;Overflow boolean
218 isNeg: .EQUATE 4 ;Negative boolean
219 state: .EQUATE 2 ;State variable
220 temp: .EQUATE 0
221 ;
222 init: .EQUATE 0 ;Enumerated values for state
223 sign: .EQUATE 1
224 digit: .EQUATE 2
225 ;
226 opcode30:LDA 0x00FE,i ;Assert d, n, s, sf, x, sx, sxf
227 STA addrMask,d
228 CALL assertAd
229 CALL setAddr ;Set address of trap operand
230 SUBSP 12,i ;Allocate storage for locals
231 LDA FALSE,i ;isOvfl := FALSE
232 STA isOvfl,s
233 LDA init,i ;state := init
234 STA state,s
235 LDA 0,i ;wordBuff := 0 for input
236 STA wordBuff,d
237 ;
238 do: CHARI byteBuff,d ;Get asciiCh
239 LDA wordBuff,d ;Set value(asciiCH)
240 ANDA 0x000F,i
241 STA valAscii,s
242 LDA wordBuff,d ;A = asciiCh throughout the loop
243 LDX state,s ;switch (state)
244 ASLX ;An address is two bytes
245 BR stateJT,x
246 ;
247 stateJT: .ADDRSS sInit
248 .ADDRSS sSign
249 .ADDRSS sDigit
250 ;
251 sInit: CPA '+',i ;if (asciiCh == '+')
252 BRNE ifMinus
253 LDX FALSE,i ;isNeg := FALSE
254 STX isNeg,s
255 LDX sign,i ;state := sign
256 STX state,s
257 BR do
258 ;
259 ifMinus: CPA '-',i ;else if (asciiCh == '-')
260 BRNE ifDigit
261 LDX TRUE,i ;isNeg := TRUE
262 STX isNeg,s
263 LDX sign,i ;state := sign
264 STX state,s
265 BR do
266 ;
267 ifDigit: CPA '0',i ;else if (asciiCh is a digit)
268 BRLT ifWhite
269 CPA '9',i
270 BRGT ifWhite
271 LDX FALSE,i ;isNeg := FALSE
272 STX isNeg,s
273 LDX valAscii,s ;total := Value(asciiCh)
274 STX total,s
275 LDX digit,i ;state := digit
276 STX state,s
277 BR do
278 ;
279 ifWhite: CPA ' ',i ;else if (asciiCh is not a space
280 BREQ do
281 CPA '\n',i ;or line feed)
282 BRNE deciErr ;exit with DECI error
283 BR do
284 ;
285 sSign: CPA '0',i ;if asciiCh (is not a digit)
286 BRLT deciErr
287 CPA '9',i
288 BRGT deciErr ;exit with DECI error
289 LDX valAscii,s ;else total := Value(asciiCh)
290 STX total,s
291 LDX digit,i ;state := digit
292 STX state,s
293 BR do
294 ;
295 sDigit: CPA '0',i ;if (asciiCh is not a digit)
296 BRLT deciNorm
297 CPA '9',i
298 BRGT deciNorm ;exit normaly
299 LDX TRUE,i ;else X := TRUE for later assignments
300 LDA total,s ;Multiply total by 10 as follows:
301 ASLA ;First, times 2
302 BRV ovfl1 ;If overflow then
303 BR L1
304 ovfl1: STX isOvfl,s ;isOvfl := TRUE
305 L1: STA temp,s ;Save 2 * total in temp
306 ASLA ;Now, 4 * total
307 BRV ovfl2 ;If overflow then
308 BR L2
309 ovfl2: STX isOvfl,s ;isOvfl := TRUE
310 L2: ASLA ;Now, 8 * total
311 BRV ovfl3 ;If overflow then
312 BR L3
313 ovfl3: STX isOvfl,s ;isOvfl := TRUE
314 L3: ADDA temp,s ;Finally, 8 * total + 2 * total
315 BRV ovfl4 ;If overflow then
316 BR L4
317 ovfl4: STX isOvfl,s ;isOvfl := TRUE
318 L4: ADDA valAscii,s ;A := 10 * total + valAscii
319 BRV ovfl5 ;If overflow then
320 BR L5
321 ovfl5: STX isOvfl,s ;isOvfl := TRUE
322 L5: STA total,s ;Update total
323 BR do
324 ;
325 deciNorm:LDA isNeg,s ;If isNeg then
326 BREQ setNZ
327 LDA total,s ;If total != 0x8000 then
328 CPA 0x8000,i
329 BREQ L6
330 NEGA ;Negate total
331 STA total,s
332 BR setNZ
333 L6: LDA FALSE,i ;else -32768 is a special case
334 STA isOvfl,s ;isOvfl := FALSE
335 ;
336 setNZ: LDBYTEX oldNZVC,s ;Set NZ according to total result:
337 ANDX 0x0001,i ;First initialize NZV to 000
338 LDA total,s ;If total is negative then
339 BRGE checkZ
340 ORX 0x0008,i ;set N to 1
341 checkZ: CPA 0,i ;If total is not zero then
342 BRNE setV
343 ORX 0x0004,i ;set Z to 1
344 setV: LDA isOvfl,s ;If not isOvfl then
345 BREQ storeFl
346 ORX 0x0002,i ;set V to 1
347 storeFl: STBYTEX oldNZVC,s ;Store the NZVC flags
348 ;
349 exitDeci:LDA total,s ;Put total in memory
350 STA opAddr,n
351 ADDSP 12,i ;Deallocate locals
352 RET0 ;Return to trap handler
353 ;
354 deciErr: CHARO '\n',i
355 LDA deciMsg,i ;Push address of message onto stack
356 STA -2,s
357 SUBSP 2,i
358 CALL prntMsg ;and print
359 STOP ;Fatal error: program terminates
360 ;
361 deciMsg: .ASCII "ERROR: Invalid DECI input\x00"
362 ;
363 ;******* Opcode 0x38
364 ;The DECO instruction.
365 ;Output format: If the operand is negative, the algorithm prints
366 ;a single '-' followed by the magnitude. Otherwise it prints the
367 ;magnitude without a leading '+'. It suppresses leading zeros.
368 ;
369 remain: .EQUATE 0 ;Remainder of value to output
370 chOut: .EQUATE 2 ;Has a character been output yet?
371 place: .EQUATE 4 ;Place value for division
372 ;
373 opcode38:LDA 0x00FF,i ;Assert i, d, n, s, sf, x, sx, sxf
374 STA addrMask,d
375 CALL assertAd
376 CALL setAddr ;Set address of trap operand
377 SUBSP 6,i ;Allocate storage for locals
378 LDA opAddr,n ;A := oprnd
379 CPA 0,i ;If oprnd is negative then
380 BRGE printMag
381 CHARO '-',i ;Print leading '-' and
382 NEGA ;make magnitude positive
383 printMag:STA remain,s ;remain := abs(oprnd)
384 LDA FALSE,i ;Initialize chOut := FALSE
385 STA chOut,s
386 LDA 10000,i ;place := 10,000
387 STA place,s
388 CALL divide ;Write 10,000's place
389 LDA 1000,i ;place := 1,000
390 STA place,s
391 CALL divide ;Write 1000's place
392 LDA 100,i ;place := 100
393 STA place,s
394 CALL divide ;Write 100's place
395 LDA 10,i ;place := 10
396 STA place,s
397 CALL divide ;Write 10's place
398 LDA remain,s ;Always write 1's place
399 ORA 0x0030,i ;Convert decimal to ASCII
400 STBYTEA byteBuff,d
401 CHARO byteBuff,d
402 RET6
403 ;
404 ;Subroutine to print the most significant decimal digit of the
405 ;remainder. It assumes that place (place2 here) contains the
406 ;decimal place value. It updates the remainder.
407 ;
408 remain2: .EQUATE 2 ;Stack addresses while executing a
409 chOut2: .EQUATE 4 ;subroutine are greater by two because
410 place2: .EQUATE 6 ;the retAddr is on the stack
411 ;
412 divide: LDA remain2,s ;A := remainder
413 LDX 0,i ;X := 0
414 divLoop: SUBA place2,s ;Division by repeated subtraction
415 BRLT writeNum ;If remainder is negative then done
416 ADDX 1,i ;X := X + 1
417 STA remain2,s ;Store the new remainder
418 BR divLoop
419 ;
420 writeNum:CPX 0,i ;If X != 0 then
421 BREQ checkOut
422 LDA TRUE,i ;chOut := TRUE
423 STA chOut2,s
424 BR printDgt ;and branch to print this digit
425 checkOut:LDA chOut2,s ;else if a previous char was output
426 BRNE printDgt ;then branch to print this zero
427 RET0 ;else return to calling routine
428 ;
429 printDgt:ORX 0x0030,i ;Convert decimal to ASCII
430 STX wordBuff,d ;for output
431 CHARO byteBuff,d
432 RET0 ;return to calling routine
433 ;
434 ;******* Opcode 0x40
435 ;The STRO instruction.
436 ;Outputs a null-terminated string from memory.
437 ;
438 opcode40:LDA 0x0016,i ;Assert d, n, sf
439 STA addrMask,d
440 CALL assertAd
441 CALL setAddr ;Set address of trap operand
442 LDA opAddr,d ;Push address of string to print
443 STA -2,s
444 SUBSP 2,i
445 CALL prntMsg ;and print
446 ADDSP 2,i
447 RET0
448 ;
449 ;******* Print subroutine
450 ;Prints a string of ASCII bytes until it encounters a null
451 ;byte (eight zero bits). Assumes one parameter, which
452 ;contains the address of the message.
453 ;
454 msgAddr: .EQUATE 2 ;Address of message to print
455 ;
456 prntMsg: LDX 0,i ;X := 0
457 LDA 0,i ;A := 0
458 prntMore:LDBYTEA msgAddr,sxf ;Test next char
459 BREQ exitPrnt ;If null then exit
460 CHARO msgAddr,sxf ;else print
461 ADDX 1,i ;X := X + 1 for next character
462 BR prntMore
463 ;
464 exitPrnt:RET0
465 ;
466 ;******* Vectors for System Memory Format
467 .ADDRSS osRAM ;User stack pointer
468 .ADDRSS wordBuff ;System stack pointer
469 .ADDRSS loader ;Loader program counter
470 .ADDRSS trap ;Trap program counter
471 ;
472 .END