Subversion Repositories Spectranet

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 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 384 winston
.include        "sysvars.inc"
23
.include        "sysdefs.inc"
24
.include        "spectranet.inc"
25
.include        "w5100_defs.inc"
26
.include        "flashconf.inc"
27
.include        "ctrlchars.inc"
28
.include        "page3.xinc"
29 27 winston
 
30 131 winston
; Locations in the data rom that need to be copied.
31 210 winston
; The jump table lives in the first 256 bytes of ROM3
32 384 winston
JUMPTABLE_COPYFROM:     equ 0x1F00
33
JUMPTABLE_SIZE:         equ 0xF8
34
UPPER_ENTRYPT:          equ 0x1FF8
35
UPPER_ENTRYPT_SIZE:     equ 0x08
36 131 winston
 
37 27 winston
; Initialization routines that are run on reset.
38
;
39
; The first thing that's done is to page in the configuration area into
40
; paging area B. This is nominally in the last page of the flash chip
41
; (page 0x20, chip 0). From this we can figure out what we're supposed
42
; to do next.
43
;
44 384 winston
.text
45
.globl J_reset
46
J_reset:
47 242 winston
        ; a delay loop to allow the rubber key machine's very slow reset
48
        ; circuit to become quiescent.
49
        ld bc, 0xFFFF
50 384 winston
.delay0:
51 242 winston
        ld (ix+7), a            ; a nice long instruction
52
        dec bc
53
        ld a, b
54
        or c
55 384 winston
        jr nz, .delay0
56 242 winston
 
57 82 winston
        ; Clear upper page.
58 328 winston
        ld sp, NMISTACK         ; use our own memory for the stack
59 77 winston
        ld hl, 0x3000           ; Clear down the fixed RAM page.
60
        ld de, 0x3001
61
        ld bc, 0xFFF
62 224 winston
        ld (hl), l
63 77 winston
        ldir
64
 
65 211 winston
        ; Set up the VFS jump point
66
        ld a, 0xC3
67
        ld (VFSJUMP), a
68
        ld a, 0x20
69
        ld (VFSJUMP+2), a
70
 
71 82 winston
        call F_clear            ; clear the screen
72
        ld hl, STR_bootmsg
73
        call F_print            ; show the boot message
74 190 winston
        ld hl, bldstr
75
        call F_print
76 82 winston
 
77
        ; Initialize some system variables that need it.
78
 
79
        ; This is a rather poor way of generating a random number seed,
80
        ; but it's the best we can do given Spectrum hardware. On power
81
        ; up or after each reset, the machine's memory will be in a slightly
82
        ; random state, so we'll CRC it to generate a seed.
83
        ld de, 23552            ; start from the sysvars area
84
        ld bc, 0x1000
85
        call F_crc16
86
        ld (v_seed), hl         ; save the CRC in the seed.
87
 
88
        ; Set all sockets to the closed state.
89 79 winston
        ld hl, v_fd1hwsock      ; set all sockets to closed
90
        ld de, v_fd1hwsock+1
91
        ld bc, MAX_FDS
92
        ld (hl), 0x80           ; MSB set = closed socket
93
        ldir
94
 
95 85 winston
        ; Set pollall() 'last file descriptor' to the first fd
96
        ld a, v_fd1hwsock % 256
97
        ld (v_lastpolled), a
98
 
99 82 winston
        ; Set an initial local port number for connect()
100
        call F_rand16
101
        set 6, h                ; make sure we start with a highish number
102
        ld (v_localport), hl    ; set initial local port address
103 77 winston
 
104 78 winston
        ; Initialize any ZX bits that need to be done.
105 190 winston
;       call F_zxinit           ; not required at present
106 78 winston
 
107 210 winston
        ld a, 0x03              ; ROM page where jumptable lives
108 131 winston
        call F_setpageA         ; and page into paging area A.
109
 
110 77 winston
        ; Initialize the jump table by copying it to our RAM.
111
        ld hl, JUMPTABLE_COPYFROM
112
        ld de, 0x3E00           ; jump table start
113
        ld bc, JUMPTABLE_SIZE
114
        ldir
115
 
116
        ; Copy the page-in instructions (for the CALL trap)
117
        ld hl, UPPER_ENTRYPT
118
        ld de, 0x3FF8
119
        ld bc, UPPER_ENTRYPT_SIZE
120
        ldir
121
 
122 95 winston
        ; Initialize the W5100 - set the MAC address and initialize
123
        ; hardware registers.
124
        call F_w5100init
125 78 winston
 
126 132 winston
        ld hl, J_rst8handler    ; Set the RST8 handler vector
127
        ld (v_rst8vector), hl
128
        ld hl, TABLE_basext     ; Set the BASIC extension table pointer
129
        ld (v_tabletop), hl
130 172 winston
 
131
        call F_initroms         ; Initialize any ROM modules we may have
132 224 winston
        call F_initfs
133 240 winston
 
134
        ; Detect machine type
135 255 winston
;       ld a, 0x03              ; ROM with detect routine
136
;       call F_setpageB
137
;       call F_machinetype
138 240 winston
 
139 340 winston
        ld a, 3                 ; Page where F_basstart lives
140
        call F_setpageB
141
        call F_basstart_setup
142
        call F_clear
143 132 winston
 
144 328 winston
        ld sp, 32767            ; lowest guaranteed stack addr
145 27 winston
        ld hl, 0                ; We're done so put 0x0000
146 172 winston
        push hl                 ; on the stack
147 384 winston
        jp PAGEOUT              ; unpage (a ret instruction)
148 27 winston
 
149 77 winston
;------------------------------------------------------------------------
150
; F_initroms
151
; Pages each 4k page of flash, checking for a boot vector in each.
152
; When a boot vector is found, that address is CALLed. That ROM then
153
; gets an opportunity to do whatever initialization it needs to do.
154
; Note this is how the W5100 actually gets configured - for the
155
; Spectranet to work at all, the Spectranet utility ROM must occupy some
156
; page somewhere in the flash chip and get initialized.
157 384 winston
.globl F_initroms
158
F_initroms:
159 110 winston
        ld b, 1         ; start from page 2 (page 0=fixed, page 1=data)
160
        ld hl, vectors  ; pointer to the valid vector table
161 384 winston
.initloop1:
162 110 winston
        inc b
163
        ld a, 0x1F
164
        cp b            ; last ROM?
165 77 winston
        ret z           ; finished
166 110 winston
        ld a, b
167 171 winston
        call F_checkromsig      ; Z = valid signature found for executable
168 384 winston
        jr z, .rominit1         ; Valid sig found
169 171 winston
        cp 0xFF                 ; empty slot
170 384 winston
        jr z, .skip1
171 171 winston
        ld (hl), 0xFF           ; "occupied but not executable"
172 384 winston
.skip1:
173 163 winston
        inc hl
174 384 winston
        jr .initloop1
175 163 winston
 
176 384 winston
.rominit1:
177 78 winston
        ; Put an entry in the vector table to indicate the ROM page has
178
        ; a valid vector table.
179 163 winston
        ld a, (0x2001)          ; fetch the ROM ID
180
        ld (hl), a              ; store it in the rom vector table
181 110 winston
        inc hl                  ; point to next entry in the table
182 163 winston
        push hl                 ; save the table address pointer
183 110 winston
        push bc                 ; save which ROM we've examined
184 77 winston
        ld hl, (ROM_INIT_VECTOR) ; get initialization vector from ROM
185 78 winston
        ld a, 0xFF
186 77 winston
        cp h                    ; does the vector point somewhere useful?
187 384 winston
        jr z, .returnaddr1      ; no - skip calling it
188
        ld de, .returnaddr1     ; get return address
189 77 winston
        push de                 ; stack it to simulate CALL
190
        jp (hl)                 ; and call it
191 384 winston
.returnaddr1:
192 110 winston
        pop bc
193 163 winston
        pop hl
194 384 winston
        jr .initloop1
195 77 winston
 
196 95 winston
;-------------------------------------------------------------------------
197 224 winston
; F_initfs
198
; Initializes filesystems.
199
; TODO: Initialize multiple filesystems.
200 384 winston
.globl F_initfs
201
F_initfs:
202 224 winston
        ld a, CONFIGPAGE        ; config page
203
        call F_setpageA
204
        ld a, (0x1000+DEF_FS_PROTO0)
205
        and a                   ; empty string?
206
        ret z
207
        cp 0xFF                 ; nothing set?
208
        ret z
209
        ld hl, STR_mounting
210
        call F_print
211
        ld hl, 0x1D00
212
        ld de, 0x3000
213
        ld bc, 0x80
214
        ldir
215
        ld ix, FSTAB
216
        ld a, 0
217
        call F_mount
218 384 winston
        jr c, .error2
219 224 winston
        ret
220 384 winston
.error2:
221 224 winston
        ld hl, 0x3000
222
        call F_itoh8
223
        ld hl, 0x3000
224
        call F_print
225
        ret
226
 
227
;-------------------------------------------------------------------------
228 95 winston
; F_w5100init
229
; Initialize the W5100 - MAC address and hardware registers.
230 384 winston
.globl F_w5100init
231
F_w5100init:
232 95 winston
        ; Set up memory pages to configure the hardware
233 110 winston
        ld a, REGPAGE           ; registers are in page 0 of the W5100
234 78 winston
        call F_setpageA         ; page it into area A
235 110 winston
        ld a, CONFIGPAGE        ; configuration page (flash)
236 95 winston
        call F_setpageB         ; paged into area B
237 78 winston
 
238 95 winston
        ld a, MR_RST            ; Perform a software reset on the W5100
239 78 winston
        ld (MR), a
240 95 winston
        xor a                   ; memory mapped mode, all options off
241 78 winston
        ld (MR), a
242
 
243 95 winston
        ld hl, 0x2000+HW_ADDRESS
244
        ld de, SHAR0            ; hardware address register
245
        ld bc, 6                ; which is 6 bytes long.
246 78 winston
        ldir
247
 
248 95 winston
        ld a, 0x55              ; initialize W5100 buffers - 2K each
249 78 winston
        ld (TMSR), a
250
        ld (RMSR), a
251 95 winston
        ld a, %11101111         ; set the IMR
252 78 winston
        ld (IMR), a
253
        ret
254 384 winston
.data
255
STR_bootmsg:
256 190 winston
        defb "Alioth Spectranet ",0
257 384 winston
        include "ver.xinc"      ; include the build number file
258
STR_mounting:
259
        defb "FS mount",NEWLINE,0
260
FSTAB:
261 224 winston
        defw 0x3001             ; TODO - proper filesystem things
262
        defw 0x3007
263
        defw 0x3030
264
        defw 0x3060
265
        defw 0x3070
266