Subversion Repositories Spectranet

[/] [trunk/] [experiments/] [testtraps.asm] - Blame information for rev 68

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 winston
; Test the execution trapper. Traps are conditionally implemented at
2
; 0x0000, 0x0038, 0x0066 (with event flip flop set), 0x0008 (with RST8EN_L
3
; set), and unconditionally at 0x3C00-0x3CFF.
4
 
5 21 winston
        include "w5100defines.asm"
6 10 winston
        org 0
7
RESET
8
        di
9
        ld sp, 0xF000   ; initialize stack pointer for our reset.
10
        jp do_reset
11
 
12
        block 0x0008-$,0
13
        ; this routine at first glance appears to 'unbalance the stack'.
14
        ; However, the 'ret' in the pageout jump will rewind the stack
15
        ; to its proper position
16
rst_8
17
        di
18 45 winston
        jp do_rst8
19 10 winston
 
20 45 winston
        block 0x10-$,0xFF
21
callbas
22
        ld (v_hlsave), hl
23
        ld (v_desave), de
24
        pop hl
25
        jp do_callbas
26
 
27 10 winston
        ; no stack munging needed to be done here, it was our interrupt
28
        ; so we're not going to run the Spectrum ROM ISR.
29
        block 0x0038-$,0
30
interrupt
31
        push hl
32
        push af
33
        ld hl, STR_int
34
        call F_print
35
        pop af
36
        pop hl
37
        jp pageout
38
 
39
        ; same as for maskable interrupts
40
        block 0x0066-$,0
41
nmi
42
        push hl
43
        push af
44
        ld hl, STR_nmi
45
        call F_print
46 48 winston
        call F_waitforkey
47 10 winston
        pop af
48
        pop hl
49
        jp pageout
50
 
51
        ; Jumping to 0x007c encounters a 'ret' instruction in the
52
        ; Spectrum ROM. The trapper resets the event FF and ROMCS FF
53
        ; when execution is encountered at this address which causes our
54
        ; ROM to page out and the Spectrum ROM to page in.
55
        block 0x007b-$,0
56
pageout
57
        ei
58
pageout_noei
59
        ret
60
 
61
do_reset
62 46 winston
        call F_resetwkspc
63 10 winston
        call F_clear            ; white screen
64 21 winston
 
65
        ld hl, 0xFFFF           ; arbitrary delay
66
.delay  dec hl
67
        ld a, h
68
        or l
69
        jr nz, .delay
70
 
71 10 winston
        ld hl, STR_reset        ; show a string
72
        call F_print
73 37 winston
 
74 18 winston
        call F_testroutine
75
 
76 20 winston
        ld hl, STR_ethinit
77
        call F_print
78
        call F_w5100init        ; init the ethernet chip
79
        ld hl, STR_ethdone
80
        call F_print
81
 
82
        call F_w5100check       ; read back config
83 23 winston
        ;call F_w5100test       ; do a test listen
84 20 winston
 
85 18 winston
        ; copy jump table to workspace ram
86
        ld hl, JTABLE1
87
        ld de, 0x3FF8
88
        ld bc, 8
89
        ldir
90 37 winston
.waitforkey
91 68 winston
        call F_waitforkey
92
        cp 1                    ; 'A'
93
        jp z, F_anexperiment
94
        cp 2
95
        jp z, F_startflashprog  ; 'S' - start flash programmer
96
        call F_inttohex8
97
        call F_print
98
;       cp 'F'                  ; want to run the flash util?
99
;       jp z, F_startflashprog  ; start the flash programmer if 'f' pressed
100 10 winston
        ld hl, 0
101
        add hl, sp              ; point hl at sp to munge stack contents
102
        ld (hl), 0              ; set current stack contents
103
        inc hl
104
        ld (hl), 0              ; to zero so 'ret' does a reset
105
        jp pageout_noei         ; and page out - Speccy ROM will now boot.
106
 
107 45 winston
do_rst8
108
        ld (v_hlsave), hl       ; save hl without disturbing stack
109
        pop hl                  ; get stack value - entry code
110
        push hl
111
        push af
112
        ld a, h                 ; check for zero - Spectrum ROM routine return
113
        or l
114
        jr z, .returnfromzxrom  ; returning from a Spectrum ROM call
115
        call F_syntax
116
        jr nz, .errexit
117 46 winston
 
118
        ld sp, (ERR_SP)         ; Reset the interpreter
119
        ld hl, ERR_NR
120
        ld (hl), 0xFF
121
        ld hl, (FLAGS)
122
        bit 7, (hl)             ; check what the interpreter was doing
123
        jr z, .stmtnext         ; Syntax checking
124
        ld hl, 0x1B7D           ; Return address is STMT-R-1
125
        push hl
126
        jp pageout
127
.stmtnext
128
        ld hl, 0x1BF4           ; Return address is STMT-NEXT
129
        push hl
130
        jp pageout
131 45 winston
 
132
.errexit
133
        pop af
134
        push hl         ; make space for our exit address in the Speccy ROM
135
        ld hl, 0        ; stack pointer must be munged to call ERROR_2
136
        add hl, sp      ; routine in the Spectrum's ROM
137
        ld (hl), ERROR_2 % 256
138
        inc hl
139
        ld (hl), ERROR_2 / 256
140
        ld hl, (CH_ADD) ; this is what the Speccy's RST 8 routine does
141
        ld (X_PTR), hl
142
        jp pageout
143
 
144
.returnfromzxrom
145
        pop af          ; restore af
146
        pop hl          ; unwind past entry code
147
        ld hl, (v_hlsave)       ; restore hl
148
        ret             ; go back to the calling routine.
149
 
150
do_callbas
151
        ld e, (hl)              ; Get the subroutine address into DE
152
        inc hl
153
        ld d, (hl)
154
        inc hl                  ; hl now is the return address
155 46 winston
        push hl                 ; put the return address back on the stack
156 45 winston
        ld hl, 0                ; entry code to RST 8
157
        push hl
158
        ld hl, 8                ; return address for Spectrum ROM to return
159
        push hl
160
        push de                 ; the actual address in ROM we want to call
161
        ld hl, (v_hlsave)       ; restore HL
162
        ld de, (v_desave)       ; restore de
163
        jp pageout              ; page out
164
 
165 10 winston
; Print utility routine.
166
F_print
167
        ld a, (hl)
168
        and a           ; test for NUL termination
169
        ret z           ; NUL encountered
170
        call putc_5by8  ; print it
171
        inc hl
172
        jr F_print
173
 
174
; Simple cls routine
175
F_clear
176
        ld hl, 16384
177
        ld de, 16385
178
        ld bc, 6144
179
        ld (hl), 0
180
        ldir
181
        ld (hl), 56     ; attribute for white
182 13 winston
        ld bc, 767
183 10 winston
        ldir
184
        xor a
185
        ld (v_column), a
186
        ld (v_rowcount), a
187
        ld hl, 16384
188
        ld (v_row), hl
189
        ret
190
 
191 46 winston
F_resetwkspc
192
        xor a
193
        ld hl, 0x3000
194
        ld de, 0x3001
195
        ld bc, 0xFFF
196
        ld (hl),a
197
        ldir
198
        ret
199
 
200 10 winston
; simple routine to show a successful call to the jump table.
201
F_jtentry
202
        di
203
        push hl
204
        push af
205
        call F_clear
206
        ld hl, STR_jptable
207
        call F_print
208
        call F_waitforkey
209
        pop af
210
        pop hl
211
        jp pageout
212
 
213 11 winston
F_calltrap1
214
        di
215
        push bc
216
        push de
217
        push hl
218
        push af
219
        call F_clear
220
        ld hl, STR_calltrap1
221
        call F_print
222
        call F_waitforkey
223
        pop af
224
        pop hl
225
        pop de
226
        pop bc
227
        jp pageout
228
 
229
F_calltrap2
230
        di
231
        push hl
232
        push af
233
        call F_clear
234
        ld hl, STR_calltrap2
235
        call F_print
236
        call F_waitforkey
237
        pop af
238
        pop hl
239
        jp pageout
240
 
241 10 winston
; Simple 'wait for the any key to get pressed' routine.
242
; Based largely on the concepts of the Spectrum's KEY-SCAN routine.
243
F_waitforkey
244
        ld bc, 0xFEFE   ; B = counter, C = port
245
.keyline
246
        in a, (c)       ; read key line
247
        cpl             ;
248
        and 0x1F        ; mask out unused bits and set flags
249
        ret nz          ; key pressed, exit the loop
250
        rlc b           ; shift counter
251
        jr c, .keyline  ; scan if lines to be scanned
252
        jr F_waitforkey ; Restart routine for another pass.
253
 
254
; Check our rather basic BASIC extension. This allows us to do a "CAT n".
255
F_syntax
256
        ; A rather rinkity dink interpreter to test handling
257
        ; of a BASIC extension.
258
.interpret
259
        ld hl, (CH_ADD)
260
        dec hl
261
        ld a, (hl)
262
        cp '*'          ; Our command?
263
        jr nz, .notmine
264
        inc hl
265
        ld a, (hl)
266 23 winston
        cp 'f'          ; Still our command?
267 46 winston
        jr z, .flash
268
        cp 's'          ; setup external interpreter?
269
        jr z, .setupext
270
        jr .notmine
271
.flash
272
        ; workaround till I find out what I'm doing wrong when
273
        ; returning to the BASIC interpreter.
274
        ld a, (v_runalready)
275
        xor 1
276
        ld (v_runalready), a
277
 
278
        ; load the flash programmer
279 10 winston
        inc hl
280 46 winston
        ld (CH_ADD), hl
281
        call nz, F_startflashprog       ; only call if xor 1 was nonzero
282
        xor a
283 10 winston
        ret
284 46 winston
.setupext
285
        inc hl
286
        ld (CH_ADD), hl
287
        ld hl, STR_setup
288
        call F_print
289
        ld hl, 0x8000
290
        ld (v_interpaddr), hl
291
        xor a
292
        ret
293 10 winston
.notmine
294 46 winston
        ld hl, (v_interpaddr)
295
        ld a, h
296
        or l
297
        jr nz, .jumpexternal
298 10 winston
        or 1            ; reset zero flag
299
        ret
300 46 winston
.jumpexternal
301
        jp (hl)
302 10 winston
 
303
; F_inttohex8 - convert 8 bit number in A. On return hl=ptr to string
304
F_inttohex8
305
        push af
306
        push bc
307
        ld hl, v_workspace
308
        ld b, a
309
        call    .Num1
310
        ld a, b
311
        call    .Num2
312
        xor a
313
        ld (hl), a      ; add null
314
        pop bc
315
        pop af
316
        ld hl, v_workspace
317
        ret
318
 
319
.Num1   rra
320
        rra
321
        rra
322
        rra
323
.Num2   or      0xF0
324
        daa
325
        add     a,0xA0
326
        adc     a,0x40
327
 
328
        ld      (hl),a
329
        inc     hl
330
        ret
331
 
332 21 winston
F_regdump
333
        push hl
334
        push de
335
        push bc
336
        push af
337
 
338
        ld a, '\n'
339
        call putc_5by8
340
 
341
        push hl
342
        ld a, h
343
        call F_inttohex8
344
        call F_print
345
        pop hl
346
        ld a, l
347
        call F_inttohex8
348
        call F_print
349
        ld a, ','
350
        call putc_5by8
351
 
352
        ld a, d
353
        call F_inttohex8
354
        call F_print
355
        ld a, e
356
        call F_inttohex8
357
        call F_print
358
        ld a, ','
359
        call putc_5by8
360
 
361
        ld a, b
362
        call F_inttohex8
363
        call F_print
364
        ld a, c
365
        call F_inttohex8
366
        call F_print
367
        ld a, ','
368
        call putc_5by8
369
 
370
        pop af
371
        push af
372
        call F_inttohex8
373
        call F_print
374
        pop bc
375
        push bc
376
        ld a, c
377
        call F_inttohex8
378
        call F_print
379
        ld a, '\n'
380
        call putc_5by8
381
 
382
        pop af
383
        pop bc
384
        pop de
385
        pop hl
386 22 winston
        ret
387 21 winston
 
388 22 winston
; hl = start address, b = byte count
389
F_hexdump
390
        push hl
391
        ld a, (hl)
392
        call F_inttohex8
393
        call F_print
394
        ld a, ' '
395
        call putc_5by8
396
        pop hl
397
        inc hl
398
        djnz F_hexdump
399
        ret
400
 
401 18 winston
; Exercise various parts of the hardware.
402
F_testroutine
403
        ld hl, STR_testwkspc
404
        call F_print
405
        ld a, 0xAA
406
        ld (0x3000), a
407
        ld a, (0x3000)
408
        cp 0xAA
409
        call nz, .oops
410
 
411 19 winston
.testpager
412
        ld hl, STR_pagetest
413
        call F_print
414
        ld a, 0x0F      ; RAM in both A and B page areas
415 37 winston
        ld bc, 0x80ED   ; port 0x80ED - (bit 15 high for 128k machines)
416
        out (c), a      ; chip selects set
417 19 winston
 
418
        ld b, 32        ; 128k has 32 x 4k pages
419
        ld c, 0xE9      ; port E9 = page area A (port %1110 1001)
420
.pageloop
421
        ld a, b
422 37 winston
        ld b, 0x80
423
        out (c), a      ; select page
424 19 winston
        ld (0x1000), a  ; set a byte in page area A
425 37 winston
        ld b, a         ; restore b
426 19 winston
        call F_inttohex8
427
        call F_print
428
        ld a, ' '
429
        call putc_5by8
430
        djnz .pageloop
431
        ld a, '\n'
432
        call putc_5by8
433
 
434
        ld hl, STR_readback
435
        call F_print
436
        ld b, 32
437
.readback
438 37 winston
        ld a, b
439
        ld b, 0x80
440
        out (c), a
441
        ld b, a
442 19 winston
        ld a, (0x1000)
443
        call F_inttohex8
444
        call F_print
445
        ld a, ' '
446
        call putc_5by8
447
        djnz .readback
448
        ld a, '\n'
449
        call putc_5by8
450
 
451 18 winston
        ld hl, STR_testsdone
452
        call F_print
453
        ret
454
 
455
.oops   ld hl, STR_oops
456
        call F_print
457
        ret
458
 
459 21 winston
; A quick test of the W5100. (Do this after init).
460
; For socket 0, nothing needs to be added to Sn_
461
F_w5100test
462
        ld hl, 0x0100   ; chip 1, page 0
463
        call F_setpageA ; page in W5100
464
.openport
465
        ld a, S_MR_TCP  ; Create a TCP socket.
466
        ld (Sn_MR), a   ; Set mode.
467
        ld a, 23        ; Port 23 (note big endian)
468
        ld (Sn_PORT1), a
469
        xor a
470
        ld (Sn_PORT0), a
471
        ld a, S_CR_OPEN ; Open the socket.
472
        ld (Sn_CR), a
473
        ld a, (Sn_SR)   ; Check status register.
474
        cp S_SR_SOCK_INIT       ; Is it?
475
        jr z, .listen   ; if so listen
476
        ld a, S_CR_CLOSE
477
        ld (Sn_CR), a   ; try again
478
        jr .openport
479
 
480
.listen
481
        ld hl, STR_open ; Display message.
482
        call F_print
483
 
484
        ld a, S_CR_LISTEN
485
        ld (Sn_CR), a   ; Set command register to listen.
486
        ld a, (Sn_SR)   ; Check status register
487
        cp S_SR_SOCK_LISTEN
488
        jr z, .listening
489
        ld a, S_CR_CLOSE
490
        ld (Sn_CR), a   ; try again
491
        jr .openport
492
 
493
.listening
494
        ld a, (Sn_SR)   ; Check status register
495
        cp S_SR_SOCK_ESTABLISHED
496
        jr nz, .listening       ; loop 'till we get a connection
497
 
498 22 winston
.rxdata
499 21 winston
        ld a, (Sn_RX_RSR0)      ; data?
500
        ld hl, Sn_RX_RSR1
501
        or (hl)                 ; test for zero
502 22 winston
        jr z, .rxdata           ; loop until data is received.
503 21 winston
.recv
504
        push hl
505
        ld hl, 0x8000           ; clear out some memory
506
        ld (hl), 0
507
        ld de, 0x8001
508
        ld bc, 0x200
509
        ldir
510
        pop hl
511
        ld de, 0x8000           ; destination address
512
        ld bc, 0x123            ; Maximum length (completely arbitrary)
513
        call F_copyrxbuf        ; Copy data
514
 
515
        ld hl, 0x8000
516 22 winston
        ld b, 10
517
        call F_hexdump
518 21 winston
 
519 22 winston
;.send
520
;       ld hl, Sn_SR
521
;       ld de, STR_open
522
;;      ld bc, STR_rxbytes-STR_open
523
;       call F_copytxbuf
524
 
525 21 winston
.closeloop
526
        ld a, (Sn_SR)
527
        cp S_SR_SOCK_CLOSE_WAIT ; has the client gone away?
528 22 winston
        jr nz, .rxdata          ; no, wait for more data
529 21 winston
 
530
.waitforclose
531
        ld a, (Sn_IR)
532
        and S_IR_DISCON
533
        jr z, .waitforclose
534
        ld hl, STR_closed
535
        call F_print
536
        ld a, S_CR_CLOSE
537
        ld (Sn_CR), a           ; close our end
538
 
539
        ret
540
 
541 20 winston
; Set paging area A. Page in HL (chip in H, page in L)
542
F_setpageA
543 37 winston
        push bc
544
        ld b, 0x80      ; upper bits of port address
545 20 winston
        ld a, (v_chipsel)
546
        and 0xFC        ; zero lower two bits
547
        or h            ; insert chip select value
548
        ld (v_chipsel), a
549 37 winston
        ld c, CHIPSEL
550
        out (c), a
551 20 winston
        ld a, l
552
        ld (v_pga), a   ; store new page number
553 37 winston
        ld c, PAGEA
554
        out (c), a      ; page it in
555
        pop bc
556 20 winston
        ret
557
 
558
; Set paging area B. As for area A.
559
F_setpageB
560 37 winston
        push bc
561
        ld b, 0x80      ; upper bits of port address
562 20 winston
        ld a, (v_chipsel)
563
        and 0xF3        ; zero upper 2 bits of nibble
564
        rl h            ; move chip select value into correct bits
565
        rl h
566
        or h            ; insert chip select value
567
        ld (v_chipsel), a
568 37 winston
        ld c, CHIPSEL
569
        out (c), a
570 20 winston
        ld a, l
571
        ld (v_pgb), a
572 37 winston
        ld c, PAGEB
573
        out (c), a      ; page it in
574
        pop bc
575 20 winston
        ret
576
 
577 23 winston
F_startflashprog
578
        ld hl, 0x0003   ; chip 0 page 3
579
        call F_setpageB
580
        ld hl, 0x2000   ; start of page area B
581
        ld de, 0xF000   ; flash programmer org addr.
582
        ld bc, ENDFLASH-STARTFLASH      ; size
583
        ldir
584
        jp 0xF000
585
 
586 68 winston
F_anexperiment
587
        call F_zxinit
588
        call F_pagezxbasic
589
.keytestloop
590
        call F_getkey
591
        cp 32
592
        jp m, .showhex
593
        call putc_5by8
594
        call F_keyup
595
        jr .keytestloop
596
.showhex
597
        push af
598
        ld a, '['
599
        call putc_5by8
600
        pop af
601
        call F_inttohex8
602
        call F_print
603
        ld a, ']'
604
        call putc_5by8
605
        jr .keytestloop
606
 
607
 
608
 
609 10 winston
; Include library routines
610
        include "print5by8.asm"
611 20 winston
        include "w5100config.asm"
612 21 winston
        include "w5100buffer.asm"
613 68 winston
        include "../rom/ui_input.asm"
614
        include "../rom/zxromcalls.asm"
615
        include "../rom/zxsysvars.asm"
616
        include "../rom/zxpaging.asm"
617 10 winston
 
618
; Strings
619
STR_reset       defb "Reset event trapped...\n", 0
620
STR_rst8        defb "RST #8 trapped...", 0
621
STR_nmi         defb "Caught NMI.\n", 0
622
STR_int         defb "Caught maskable interrupt.\n",0
623
STR_jptable     defb "Jump table entry point used.\n", 0
624
STR_ourcmd      defb "\nA command for us has been recognised.\n",0
625 11 winston
STR_calltrap1   defb "Calltrap 1 - CALL 0x3FF8 trapped.\n",0
626
STR_calltrap2   defb "Calltrap 2 - CALL 0x3FFB trapped.\n",0
627 18 winston
STR_testwkspc   defb "Testing workspace...\n",0
628
STR_oops        defb "Test failed.\n",0
629
STR_testsdone   defb "Tests complete.\n",0
630 19 winston
STR_pagetest    defb "Testing pager: ",0
631
STR_readback    defb "Readback     : ",0
632 20 winston
STR_ethinit     defb "Initializing W5100...",0
633
STR_ethdone     defb "Done.\n", 0
634 21 winston
STR_open        defb "\nListening on port 23\n",0
635
STR_rxbytes     defb "Received bytes = ",0
636
STR_closed      defb "\nSocket closed.\n",0
637 46 winston
STR_setup       defb "External interpreter set to 0x8000\n",0
638 10 winston
 
639 11 winston
JTABLE1 jp F_calltrap1
640
JTABLE2 jp F_calltrap2
641
        jr JTABLE1
642 10 winston
 
643 18 winston
 
644
        block 0x0B00-$,0xFF     ; 0xFF wears the flash chip less
645
        include "rclookup.asm"  ; row/column lookup table
646
        include "charset.asm"
647
 
648 23 winston
        block 0x3000-$,0xFF
649
STARTFLASH
650
        incbin "../flash/flashprog.out"
651
ENDFLASH        defb 0
652 18 winston
 
653 20 winston
; Workspace defs.
654
v_column        equ 0x3F00      ; Current column for print routine
655
v_row           equ 0x3F01      ; Current row address for print routine
656
v_rowcount      equ 0x3F03      ; Current row number for print routine
657
v_pr_wkspc      equ 0x3F04      ; Print routine workspace
658
v_workspace     equ 0x3F05      ; General purpose workspace
659
v_pga           equ 0x3F10      ; Paging area A page
660
v_pgb           equ 0x3F11      ; Paging area B page
661
v_chipsel       equ 0x3F12      ; Chip select values
662 10 winston
 
663 51 winston
; Put these elsewhere so they don't collide with ROM testing
664
v_sockptr       equ 0x3000      ; Pointer to socket register (2 bytes)
665
v_copylen       equ 0x3002      ; Length to copy
666
v_copied        equ 0x3004      ; Wrapped copied so far
667
v_hlsave        equ 0x3006      ; save hl for callbas
668
v_desave        equ 0x3008
669
v_interpaddr    equ 0x300A      ; extra interpreter to call
670
v_runalready    equ 0x300C      ; 'run already' flag
671 68 winston
v_bankm         equ 0x300D
672
v_bank678       equ 0x300E
673 51 winston
 
674 10 winston
; Spectrum ROM entry points
675
ERROR_2         equ 0x0053
676 46 winston
ERR_SP          equ 23613
677
ERR_NR          equ 23610
678
FLAGS           equ 23611
679 10 winston
 
680
; Spectrum system variables
681
CH_ADD          equ 23645
682
X_PTR           equ 23647
683
 
684 20 winston
; various definitions
685
CHIPSEL         equ 0xED
686
PAGEA           equ 0xE9
687
PAGEB           equ 0xEB
688 68 winston
CALLBAS         equ 0x10