Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [rom/] [romconfigmain.asm] - Blame information for rev 384

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 163 winston
;The MIT License
2
;
3
;Copyright (c) 2008 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22
 
23
; ROM configuration utility main routine.
24 382 winston
.include        "spectranet.inc"
25
.include        "ctrlchars.inc"
26
.include        "sysvars.inc"
27 384 winston
.include        "sockdefs.inc"
28 163 winston
 
29
;--------------------------------------------------------------------------
30 169 winston
; F_romconfigmain: Main loop
31 382 winston
.section main
32
.globl F_romconfigmain
33
F_romconfigmain:
34 169 winston
        call CLEAR42
35
        ld hl, STR_installed
36
        call PRINT42
37
        call F_showroms
38
        call F_configmenu
39
        jr z, F_romconfigmain
40
 
41
        ; page back in caller
42
        ld a, 0x02              ; utility ROM
43
        call SETPAGEB
44
        ret
45 382 winston
.text
46 169 winston
;--------------------------------------------------------------------------
47 163 winston
; F_showroms - Shows the current available ROMs.
48 382 winston
.globl F_showroms
49
F_showroms:
50 163 winston
        ld a, 0x02              ; first valid ROM slot
51 382 winston
.disploop2:
52 163 winston
        call SETPAGEB
53
        push af
54
        ld a, (0x2000)          ; check signature byte
55
        cp 0xFF                 ; end?
56 382 winston
        jr z, .exit2            ; exit routine
57 163 winston
        cp 0xAA                 ; ROM module?
58 382 winston
        jr nz, .putdatarom2     ; no, list as "data"
59 163 winston
        pop af                  ; get ROM number
60
        push af
61 382 winston
        call .printid2          ; print its id
62 163 winston
        ld hl, (0x200E)         ; get the identity string
63
        call PRINT42            ; print the identity
64 382 winston
        ld a, NEWLINE
65 163 winston
        call PUTCHAR42
66
        pop af
67
        inc a
68 382 winston
        jr .disploop2
69
.exit2:
70 163 winston
        pop af
71
        ret
72
 
73 382 winston
.putdatarom2:
74 163 winston
        pop af
75
        push af
76 382 winston
        call .printid2
77 163 winston
        ld hl, STR_datarom
78
        call PRINT42
79
        pop af
80
        inc a
81 382 winston
        jr .disploop2
82 163 winston
 
83 382 winston
.printid2:
84 163 winston
        ld hl, v_workspace      ; workspace to load with value
85
        call ITOH8
86
        ld a, '['
87
        call PUTCHAR42
88
        ld hl, v_workspace
89
        call PRINT42            ; print the ROM number in hex
90
        ld a, ']'
91
        call PUTCHAR42
92
        ld a, ' '
93
        call PUTCHAR42
94
        ret
95
 
96
;-------------------------------------------------------------------------
97
; F_findfirstfreepage
98
; Finds the first free ROM page, returning it in A.
99
; Search starts from the first user page, page 0x04. Returns with the
100
; carry flag set if no free pages are available.
101 382 winston
.globl F_findfirstfreepage
102
F_findfirstfreepage:
103 163 winston
        ld a, 0x04
104 382 winston
.loop3:
105 166 winston
        call SETPAGEB
106 163 winston
        ex af, af'
107
        ld a, (0x2000)
108
        cp 0xFF                 ; FF = free page
109 382 winston
        jr z, .found3
110 163 winston
        ex af, af'
111
        cp 0x1F                 ; Last page?
112 382 winston
        jr z, .nospace3
113 163 winston
        inc a
114 382 winston
        jr .loop3
115
.nospace3:
116 163 winston
        scf
117
        ret
118 382 winston
.found3:
119
        ex af, af'
120 165 winston
        and a                   ; make sure carry is reset
121
        ret
122 163 winston
 
123
;-----------------------------------------------------------------------
124
; F_loader
125
; Loads some data into RAM.
126 382 winston
.globl F_loader
127
F_loader:
128 163 winston
        ; Page in some RAM for the data to land.
129
        call SETPAGEA
130
 
131
        call CLEAR42
132
 
133
        ld c, SOCK_STREAM       ; open a TCP socket
134
        call SOCKET             ; file descriptor in A
135 382 winston
        jp c, .borked4          ; or c set if failed
136 163 winston
        ld (v_sockfd), a        ; save the fd
137
 
138
        ld de, 2000             ; port 2000
139
        call BIND               ; bind to the port
140 382 winston
        jp c, .borked4
141 163 winston
 
142
        ld a, (v_sockfd)        ; socket we want to listen on
143
        call LISTEN             ; listen
144 382 winston
        jr c, .borked4
145 163 winston
 
146
        ; Display an informative message to the user showing the
147
        ; IP and port we are listening on.
148
        ld hl, STR_send
149
        call PRINT42
150
        ld de, buf_workspace    ; where to deposit our IP address
151
        call GET_IFCONFIG_INET
152
        ld hl, buf_workspace
153
        ld de, buf_workspace+4
154
        call LONG2IPSTRING
155
        ld hl, buf_workspace+4
156
        call PRINT42
157
        ld hl, STR_port
158
        call PRINT42
159
 
160
        ; Wait for a connection.
161
        ld a, (v_sockfd)
162
        call ACCEPT             ; block until something connects
163 382 winston
        jr c, .borked4
164 163 winston
        ld (v_connfd), a        ; save the connection file descriptor
165
 
166
        ; Get the first 4 bytes which contains the start address and
167
        ; data length. We are actually going to ignore the start
168
        ; address but to be compatible with the "ethup" utility we
169
        ; still get it.
170
        ld de, buf_workspace    ; where to store
171
        ld bc, 4                ; how many bytes
172
        call RECV               ; block till we get them
173
        call F_printxfinfo      ; print information about the data
174
 
175
        ; Receive the data.
176
        ld de, 0x1000           ; current address to write to
177 218 winston
        ld (buf_workspace), de  ; save current pointer
178 382 winston
.recvloop4:
179 163 winston
        ld a, (v_connfd)
180
        ld bc, 1024             ; receive up to 1K at a time
181
        call RECV
182 382 winston
        jr c, .borked4
183 163 winston
        ld hl, (buf_workspace+2) ; get remaining length
184
        sbc hl, bc
185
        ld a, h                 ; are we done yet?
186
        or l
187 382 winston
        jr z, .recvdone4
188 163 winston
        ld (buf_workspace+2), hl ; save remaining length
189
        ld hl, (buf_workspace)  ; get current pointer
190
        add hl, bc              ; increment it
191
        ld (buf_workspace), hl  ; save it
192
        ex de, hl
193
        ld a, '.'               ; progress marker
194
        call PUTCHAR42
195 382 winston
        jr .recvloop4           ; get the next block
196
.recvdone4:
197 163 winston
        ld a, (v_connfd)        ; close the connection
198
        call CLOSE
199
        ld a, (v_sockfd)
200
        call CLOSE
201
        ret
202 382 winston
.borked4:
203 163 winston
        ld hl, buf_workspace
204
        call ITOH8
205
        ld hl, STR_borked
206
        call PRINT42
207
        ld hl, buf_workspace
208
        call PRINT42
209 165 winston
        scf
210
        ret
211 163 winston
 
212
; internal function for the above - print info of what's being tx'd to us.
213 382 winston
.globl F_printxfinfo
214
F_printxfinfo:
215 163 winston
        ld hl, STR_est
216
        call PRINT42
217
        ld hl, STR_len
218
        call PRINT42
219
        ld a, (buf_workspace+3)
220
        ld hl, buf_workspace+4
221
        call ITOH8
222
        ld a, (buf_workspace+2)
223
        call ITOH8
224
        ld hl, buf_workspace+4
225
        call PRINT42
226 382 winston
        ld a, NEWLINE
227 163 winston
        call PUTCHAR42
228
        ret
229
 
230
;-------------------------------------------------------------------------
231
; F_romconfigmenu
232
; Displays the ROM configuration menu.
233 382 winston
.globl F_configmenu
234
F_configmenu:
235 163 winston
        ld hl, STR_menutitle
236
        call PRINT42
237
        ld hl, MENU_romconfig
238
        call F_genmenu
239
        ld hl, MENU_romconfig
240
        call F_getmenuopt
241
        ret
242
 
243 165 winston
;-------------------------------------------------------------------------
244
; F_addmodule
245
; Adds a new module to the last slot in ROM, assuming there's one available
246 382 winston
.globl F_addmodule
247
F_addmodule:
248 165 winston
        call F_findfirstfreepage        ; Find the ROM page for the module
249
        jp c, J_noroom                  ; Report "no room" if C is set
250
        call SETPAGEB                   ; page it into area B
251 166 winston
        ld a, 0xC3                      ; page to fill in RAM
252 165 winston
        call F_loader                   ; fetch data over the network
253
        jp c, F_waitforkey              ; bale out now, error receiving
254
        ld hl, STR_writingmod           ; tell the user
255
        call PRINT42
256
        ld hl, v_workspace              ; hex conversion
257
        ld a, (v_pgb)                   ; of current page B
258
        call ITOH8                      ; convert to hex string
259
        ld hl, v_workspace
260
        call PRINT42                    ; print it
261 166 winston
        ld hl, 0x1000                   ; program a 4K block
262
        ld de, 0x2000
263
        ld bc, 0x1000
264
        di
265
        call F_FlashWriteBlock
266
        ei
267 167 winston
        jp c, J_writeborked
268 165 winston
        jp F_waitforkey
269
 
270 166 winston
;-------------------------------------------------------------------------
271
; F_repmodule
272
; Replaces a module.
273 382 winston
.globl F_repmodule
274
F_repmodule:
275 166 winston
        ld hl, STR_entermod
276
        call PRINT42
277
        call F_getromnum                ; ask for the ROM id
278
        ret z                           ; user baled on us
279
        ld (v_workspace), a             ; the ROM that will be replaced.
280
        and 0xFC                        ; mask out bottom two bits to find
281
        ld (v_workspace + 1), a         ; the sector, store it for later
282
        call F_copysectortoram          ; copy the flash sector
283
        ld a, (v_workspace)             ; calculate the RAM page to use
284
        and 0x03                        ; get position in the sector
285 382 winston
        add a, 0xDC                     ; add RAM page number
286 166 winston
        call F_loader                   ; get the new data over the net
287
        ld a, (v_workspace + 1)         ; retrieve sector page
288
        di
289
        call F_FlashEraseSector
290
        jr c, J_eraseborked             ; oops!
291
        ld a, (v_workspace + 1)
292
        call F_writesector              ; write the new contents of RAM
293
        ei
294
        jr c, J_writeborked
295
        jp F_waitforkey
296
 
297
;------------------------------------------------------------------------
298
; Report flash write/erase failures
299 382 winston
J_eraseborked:
300 166 winston
        ld hl, STR_erasebork
301
        call PRINT42
302 167 winston
        scf
303 163 winston
        ret
304 167 winston
 
305
;-----------------------------------------------------------------------
306
; F_removepage
307
; Removes a page from a sector of flash, shuffling all the ROMs down.
308
; Page to remove should be in (v_workspace), and A should have the sector
309
; first page.
310 382 winston
.globl F_removepage
311
F_removepage:
312 167 winston
        ld b, 3                         ; 3 pages to copy to RAM
313
        ex af, af'
314
        ld a, 0xDC                      ; RAM page to start with
315 382 winston
.loop9:
316 167 winston
        push bc
317
        call F_setpageB                 ; page in RAM destination
318
        inc a
319
        ex af, af'
320
        ld hl, v_workspace
321
        cp (hl)                         ; is this the page to delete?
322 382 winston
        jr nz, .noskip9
323 167 winston
        inc a                           ; if so skip it
324 382 winston
.noskip9:
325 167 winston
        call F_setpageA                 ; page in ROM source
326
        inc a
327
        ex af, af'
328
        ld hl, 0x1000                   ; ROM source addr
329
        ld de, 0x2000                   ; RAM dest addr
330
        ld bc, 0x1000
331
        ldir
332
        pop bc
333 382 winston
        djnz .loop9
334 167 winston
 
335
        call F_setpageB                 ; page in the final RAM page
336
        ld hl, 0x2000
337
        ld de, 0x2001
338
        ld bc, 0xFFF
339
        ld (hl), 0xFF                   ; and fill it with 0xFF (empty flash)
340
        ldir
341
 
342
        ex af, af'                      ; get the flash page pointer back
343
        dec a                           ; and turn it back into the
344
        and 0xFC                        ; first page in the sector
345
        push af                         ; save it
346
        di
347
        call F_FlashEraseSector         ; erase it
348 382 winston
        jr c, .fail9
349 167 winston
        pop af
350
        call F_writesector              ; write the modified sector back
351
        ei
352
        jr c, J_writeborked
353
        ret
354 382 winston
.fail9:
355 167 winston
        ei
356
        pop af                          ; restore stack
357
        jr J_eraseborked
358
 
359
;-----------------------------------------------------------------------
360
; Jump here to display a write failed message.
361 382 winston
J_writeborked:
362 166 winston
        ld hl, STR_writebork
363
        call PRINT42
364 167 winston
        scf
365 166 winston
        ret
366
 
367 167 winston
;-----------------------------------------------------------------------
368
; F_remmodule: Removes a module.
369 382 winston
.globl F_remmodule
370
F_remmodule:
371 167 winston
        ld hl, STR_delrom
372
        call PRINT42
373
        call F_getromnum                ; ask for the ROM id
374
        ret z                           ; user baled on us
375
        ex af, af'
376
        dec a
377
        ld (v_workspace+2), a           ; save the first free page - 1
378
        ex af, af'
379
        ld (v_workspace), a             ; make a copy of the user selection
380
        and 0xFC                        ; find the sector
381
        ld (v_workspace+1), a           ; store the sector's first page num
382
        ld hl, STR_erasing
383
        call PRINT42
384
        ld a, (v_workspace+1)
385
        call F_removepage               ; remove the selected ROM
386
        ret c                           ; leave if it failed
387
        ld hl, STR_eraseok
388
        call PRINT42
389
 
390
        ; If there are further occupied sectors after the one from which
391
        ; the ROM page was just removed, we need to take a page from the
392
        ; last sector and use it to fill in the hole we just made.
393
        ld a, (v_workspace+2)
394
        and 0xFC                        ; what sector?
395
        ld b, a
396
        ld a, (v_workspace+1)
397
        cp b                            ; no more sectors after this one
398 169 winston
        ret z
399 167 winston
 
400
        ld hl, STR_defragment           ; report progress
401
        call PRINT42
402
        ld a, (v_workspace+2)           ; page in the relevant page
403
        call F_setpageA                 ; to page area A
404
        ld a, 0xDC
405
        call F_setpageB                 ; RAM in B
406
        ld hl, 0x1000                   ; copy
407
        ld de, 0x2000
408
        ld bc, 0x1000
409
        ldir
410
        ld a, (v_workspace+1)           ; get the sector with the hole
411
        or 0x03                         ; and the page of the hole to fill
412
        call F_setpageB
413
        ld hl, 0x1000                   ; flash it into the hole
414
        ld de, 0x2000
415
        ld bc, 0x1000
416
        di
417
        call F_FlashWriteBlock
418
        ei
419
        jr c, J_writeborked
420
        ld a, (v_workspace + 2)         ; get the page of the data we copied
421
        call F_removepage               ; and remove it
422 169 winston
        xor a                           ; reset Z
423 163 winston
        ret
424 167 winston
 
425
;----------------------------------------------------------------------
426
; F_exit: Exit the ROM utility.
427 382 winston
.globl F_exit
428
F_exit:
429 169 winston
        or 1                            ; reset Z flag
430 163 winston
        ret
431
 
432
;-------------------------------------------------------------------------
433 165 winston
; J_noroom
434 382 winston
J_noroom:
435 165 winston
        ld hl, STR_noroom
436
        call PRINT42
437
;-------------------------------------------------------------------------
438
; 'Press x to exit'
439 382 winston
.globl F_waitforkey
440
F_waitforkey:
441 165 winston
        ld hl, STR_xtoexit
442
        call PRINT42
443 382 winston
.waitforkey12:
444 165 winston
        call GETKEY
445
        cp 'x'
446 382 winston
        jr nz, .waitforkey12
447 169 winston
        xor a                   ; set Z flag
448 165 winston
        ret
449
 
450
;-------------------------------------------------------------------------
451 166 winston
; F_getromnum
452 167 winston
; Ask the user for a ROM slot (hex value), return it in A
453
; Note. First free page is also returned in A'
454 382 winston
.globl F_getromnum
455
F_getromnum:
456 166 winston
        ld c, 3                 ; buffer is 3 bytes long (2 + null)
457
        ld de, v_workspace
458
        call INPUTSTRING
459
        ld hl, v_workspace
460
        ld a, (hl)
461
        and a                   ; nothing entered?
462
        ret z
463
        call HTOI8              ; convert hex string pointed to by hl
464
        cp 4                    ; must be greater than 4
465 382 winston
        jr c, .invalid13
466 166 winston
        push af                 ; save the ROM number selected
467
        call F_findfirstfreepage
468
        pop bc                  ; get AF back into BC for comparison
469
        cp b                    ; b should be <= a
470 382 winston
        jr c, .invalid13
471 167 winston
        ex af, af'              ; keep first free page in A'
472 166 winston
        ld a, b                 ; move result into A
473 167 winston
        or a                    ; make sure Z is not set
474 166 winston
        ret
475 382 winston
.invalid13:
476 166 winston
        ld hl, STR_notvalid
477
        call PRINT42
478
        jr F_getromnum
479
 
480
;-------------------------------------------------------------------------
481
; F_copysectortoram
482
; Copies 4 pages of flash to RAM.
483
; Parameter: A = first page.
484 382 winston
.globl F_copysectortoram
485
F_copysectortoram:
486 167 winston
        ex af, af'                      ; save ROM page
487 166 winston
        ld a, 0xDC                      ; first RAM page
488
        ld b, 4                         ; pages to copy
489 382 winston
.copyloop14:
490 166 winston
        push bc
491 167 winston
        call F_setpageB                 ; RAM into area B
492 166 winston
        inc a
493 167 winston
        ex af, af'                      ; ROM page into A
494
        call F_setpageA                 ; page it in
495 166 winston
        inc a
496 167 winston
        ex af, af'                      ; for the next iteration.
497 166 winston
        ld hl, 0x1000                   ; copy the page
498
        ld de, 0x2000
499
        ld bc, 0x1000
500
        ldir
501
        pop bc
502 382 winston
        djnz .copyloop14
503 166 winston
        ret
504
 
505 382 winston
.globl F_debugpages
506
F_debugpages:
507 167 winston
        push af
508
        ex af, af'
509
        push af
510
 
511
        ex af, af'
512
        ld hl, v_workspace+2
513
        call ITOH8
514
        ld hl, STR_accum
515
        call PRINT42
516
        ld hl, v_workspace+2
517
        call PRINT42
518
 
519
        pop af
520
        push af
521
        ld hl, v_workspace+2
522
        call ITOH8
523
        ld hl, STR_accum1
524
        call PRINT42
525
        ld hl, v_workspace+2
526
        call PRINT42
527
 
528
        ld a, (v_workspace)
529
        ld hl, v_workspace+2
530
        call ITOH8
531
        ld hl, STR_workspace
532
        call PRINT42
533
        ld hl, v_workspace+2
534
        call PRINT42
535
 
536
        ld a, (v_workspace+1)
537
        ld hl, v_workspace+2
538
        call ITOH8
539
        ld hl, STR_workspace1
540
        call PRINT42
541
        ld hl, v_workspace+2
542
        call PRINT42
543
 
544
        call F_waitforkey
545
        pop af
546
        ex af, af'
547
        pop af
548
        ret
549 382 winston
.data
550
STR_workspace:  defb NEWLINE,"workspace + 0: ",0
551
STR_workspace1: defb NEWLINE,"workspace + 1: ",0
552
STR_accum:      defb NEWLINE,"A register   : ",0
553
STR_accum1:     defb NEWLINE,"A' register  : ",0
554 167 winston
 
555 166 winston
;-------------------------------------------------------------------------
556 163 winston
; Definitions.
557 382 winston
MENU_romconfig:
558 163 winston
        defw STR_addmodule,F_addmodule
559
        defw STR_repmodule,F_repmodule
560
        defw STR_remmodule,F_remmodule
561
        defw STR_exit,F_exit
562
        defw 0
563