]> git.sesse.net Git - pistorm/blob - platforms/amiga/piscsi/device_driver_amiga/bootrom.s
25721255083be884cc49a8aaaeaccca5ced572cb
[pistorm] / platforms / amiga / piscsi / device_driver_amiga / bootrom.s
1 **
2 ** Sample autoboot code fragment
3 **
4 ** These are the calling conventions for the Diag routine
5 **
6 ** A7 -- points to at least 2K of stack
7 ** A6 -- ExecBase
8 ** A5 -- ExpansionBase
9 ** A3 -- your board's ConfigDev structure
10 ** A2 -- Base of diag/init area that was copied
11 ** A0 -- Base of your board
12 **
13 ** Your Diag routine should return a non-zero value in D0 for success.
14 ** If this value is NULL, then the diag/init area that was copied
15 ** will be returned to the free memory pool.
16 **
17
18     INCLUDE "exec/types.i"
19     INCLUDE "exec/nodes.i"
20     INCLUDE "exec/resident.i"
21     INCLUDE "libraries/configvars.i"
22     INCLUDE "libraries/expansionbase.i"
23
24     ; LVO's resolved by linking with library amiga.lib
25     XREF   _LVOFindResident
26
27 ROMINFO     EQU      0
28 ROMOFFS     EQU     $4000
29
30 * ROMINFO defines whether you want the AUTOCONFIG information in
31 * the beginning of your ROM (set to 0 if you instead have PALS
32 * providing the AUTOCONFIG information instead)
33 *
34 * ROMOFFS is the offset from your board base where your ROMs appear.
35 * Your ROMs might appear at offset 0 and contain your AUTOCONFIG
36 * information in the high nibbles of the first $40 words ($80 bytes).
37 * Or, your autoconfig ID information may be in a PAL, with your
38 * ROMs possibly being addressed at some offset (for example $2000)
39 * from your board base.  This ROMOFFS constant will be used as an
40 * additional offset from your configured board address when patching
41 * structures which require absolute pointers to ROM code or data.
42
43 *----- We'll store Version and Revision in serial number
44 VERSION             EQU 37              ; also the high word of serial number
45 REVISION            EQU 1               ; also the low word of serial number
46
47 * See the Addison-Wesley Amiga Hardware Manual for more info.
48     
49 MANUF_ID            EQU 2011            ; CBM assigned (2011 for hackers only)
50 PRODUCT_ID          EQU 1               ; Manufacturer picks product ID
51
52 BOARDSIZE           EQU $10000          ; How much address space board decodes
53 SIZE_FLAG           EQU 3               ; Autoconfig 3-bit flag for BOARDSIZE
54                                         ;   0=$800000(8meg)  4=$80000(512K)
55                                         ;   1=$10000(64K)    5=$100000(1meg)
56                                         ;   2=$20000(128K)   6=$200000(2meg)
57                                         ;   3=$40000(256K)   7=$400000(4meg)
58                 CODE
59
60 ; Exec stuff
61 AllocMem        EQU -198
62 InitResident    EQU -102
63 FindResident    EQU -96
64 OpenLibrary     EQU -552
65 CloseLibrary    EQU -414
66 OpenResource    EQU -$1F2
67 AddResource     EQU -$1E6
68 Enqueue         EQU -$10E
69 AddMemList      EQU -$26A
70
71 ; Expansion stuff
72 MakeDosNode     EQU -144
73 AddDosNode      EQU -150
74 AddBootNode     EQU -36
75
76 ; PiSCSI stuff
77 PiSCSIAddr1     EQU $80000010
78 PiSCSIAddr2     EQU $80000014
79 PiSCSIAddr3     EQU $80000018
80 PiSCSIAddr4     EQU $8000001C
81 PiSCSIDebugMe   EQU $80000020
82 PiSCSIDriver    EQU $80000040
83 PiSCSINextPart  EQU $80000044
84 PiSCSIGetPart   EQU $80000048
85 PiSCSIGetPrio   EQU $8000004C
86 PiSCSIGetFS     EQU $80000060
87 PiSCSINextFS    EQU $80000064
88 PiSCSICopyFS    EQU $80000068
89 PiSCSIFSSize    EQU $8000006C
90 PiSCSISetFSH    EQU $80000070
91 PiSCSIDbg1      EQU $80001010
92 PiSCSIDbg2      EQU $80001014
93 PiSCSIDbg3      EQU $80001018
94 PiSCSIDbg4      EQU $8000101C
95 PiSCSIDbg5      EQU $80001020
96 PiSCSIDbg6      EQU $80001024
97 PiSCSIDbg7      EQU $80001028
98 PiSCSIDbg8      EQU $8000102C
99 PiSCSIDbgMsg    EQU $80001000
100
101 *******  RomStart  ***************************************************
102 **********************************************************************
103
104 RomStart:
105
106 *******  DiagStart  **************************************************
107 DiagStart:  ; This is the DiagArea structure whose relative offset from
108             ; your board base appears as the Init Diag vector in your
109             ; autoconfig ID information.  This structure is designed
110             ; to use all relative pointers (no patching needed).
111             dc.b    DAC_WORDWIDE+DAC_CONFIGTIME    ; da_Config
112             dc.b    0                              ; da_Flags
113             dc.w    $4000              ; da_Size
114             dc.w    DiagEntry-DiagStart            ; da_DiagPoint
115             dc.w    BootEntry-DiagStart            ; da_BootPoint
116             dc.w    DevName-DiagStart              ; da_Name
117             dc.w    0                              ; da_Reserved01
118             dc.w    0                              ; da_Reserved02
119
120 *******  Resident Structure  *****************************************
121 Romtag:
122             dc.w    RTC_MATCHWORD      ; UWORD RT_MATCHWORD
123 rt_Match:   dc.l    Romtag-DiagStart   ; APTR  RT_MATCHTAG
124 rt_End:     dc.l    EndCopy-DiagStart  ; APTR  RT_ENDSKIP
125             dc.b    RTW_COLDSTART      ; UBYTE RT_FLAGS
126             dc.b    VERSION            ; UBYTE RT_VERSION
127             dc.b    NT_DEVICE          ; UBYTE RT_TYPE
128             dc.b    20                 ; BYTE  RT_PRI
129 rt_Name:    dc.l    DevName-DiagStart  ; APTR  RT_NAME
130 rt_Id:      dc.l    IdString-DiagStart ; APTR  RT_IDSTRING
131 rt_Init:    dc.l    Init-RomStart      ; APTR  RT_INIT
132
133
134 ******* Strings referenced in Diag Copy area  ************************
135 DevName:    dc.b    'pi-scsi.device',0,0                      ; Name string
136 IdString    dc.b    'PISCSI v0.8',0   ; Id string
137
138 DosName:        dc.b    'dos.library',0                ; DOS library name
139 ExpansionName:  dc.b    "expansion.library",0
140 LibName:        dc.b    "pi-scsi.device",0,0
141
142 DosDevName: dc.b    'ABC',0        ; dos device name for MakeDosNode()
143                                    ;   (dos device will be ABC:)
144
145             ds.w    0              ; word align
146
147 *******  DiagEntry  **************************************************
148 **********************************************************************
149 *
150 *   success = DiagEntry(BoardBase,DiagCopy, configDev)
151 *   d0                  a0         a2                  a3
152 *
153 *   Called by expansion architecture to relocate any pointers
154 *   in the copied diagnostic area.   We will patch the romtag.
155 *   If you have pre-coded your MakeDosNode packet, BootNode,
156 *   or device initialization structures, they would also need
157 *   to be within this copy area, and patched by this routine.
158 *
159 **********************************************************************
160
161 DiagEntry:
162             align 2
163             nop
164             nop
165             nop
166             move.l #1,PiSCSIDebugMe
167             move.l a3,PiSCSIAddr1
168             nop
169             nop
170             nop
171             nop
172             nop
173             nop
174
175             lea      patchTable-RomStart(a0),a1   ; find patch table
176             adda.l   #ROMOFFS,a1                  ; adjusting for ROMOFFS
177
178 * Patch relative pointers to labels within DiagCopy area
179 * by adding Diag RAM copy address.  These pointers were coded as
180 * long relative offsets from base of the DiagArea structure.
181 *
182 dpatches:
183             move.l   a2,d1           ;d1=base of ram Diag copy
184 dloop:
185             move.w   (a1)+,d0        ;d0=word offs. into Diag needing patch
186             bmi.s    bpatches        ;-1 is end of word patch offset table
187             add.l    d1,0(a2,d0.w)   ;add DiagCopy addr to coded rel. offset
188             bra.s    dloop
189
190 * Patches relative pointers to labels within the ROM by adding
191 * the board base address + ROMOFFS.  These pointers were coded as
192 * long relative offsets from RomStart.
193 *
194 bpatches:
195             move.l   a0,d1           ;d1 = board base address
196             add.l    #ROMOFFS,d1     ;add offset to where your ROMs are
197 rloop:
198             move.w   (a1)+,d0        ;d0=word offs. into Diag needing patch
199             bmi.s   endpatches       ;-1 is end of patch offset table
200             add.l   d1,0(a2,d0.w)    ;add ROM address to coded relative offset
201             bra.s   rloop
202
203 endpatches:
204             moveq.l #1,d0           ; indicate "success"
205             rts
206
207
208 *******  BootEntry  **************************************************
209 **********************************************************************
210
211 BootEntry:
212             align 2
213             move.l #2,PiSCSIDebugMe
214             lea DosName(pc),a1
215             jsr FindResident(a6)
216             tst.l d0
217             beq.b .End
218             move.l d0,a0
219             move.l RT_INIT(a0),a0
220             jmp (a0)
221 .End
222             moveq.l #1,d0           ; indicate "success"
223             rts
224
225 *
226 * End of the Diag copy area which is copied to RAM
227 *
228 EndCopy:
229 *************************************************************************
230
231 *************************************************************************
232 *
233 *   Beginning of ROM driver code and data that is accessed only in
234 *   the ROM space.  This must all be position-independent.
235 *
236
237 patchTable:
238 * Word offsets into Diag area where pointers need Diag copy address added
239             dc.w   rt_Match-DiagStart
240             dc.w   rt_End-DiagStart
241             dc.w   rt_Name-DiagStart
242             dc.w   rt_Id-DiagStart
243             dc.w   -1
244
245 * Word offsets into Diag area where pointers need boardbase+ROMOFFS added
246             dc.w   rt_Init-DiagStart
247             dc.w   -1
248
249 *******  Romtag InitEntry  **********************************************
250 *************************************************************************
251
252 Init:       ; After Diag patching, our romtag will point to this
253             ; routine in ROM so that it can be called at Resident
254             ; initialization time.
255             ; This routine will be similar to a normal expansion device
256             ; initialization routine, but will MakeDosNode then set up a
257             ; BootNode, and Enqueue() on eb_MountList.
258             ;
259             align 2
260             move.l a6,-(a7)             ; Push A6 to stack
261             move.w #$00B8,$dff09a       ; Disable interrupts during init
262             move.l  #3,PiSCSIDebugMe
263             move.l a3,PiSCSIAddr4
264
265             movea.l 4,a6
266
267             move.l $10000040,d1
268             move.l #$feffeeff,$10000040
269             move.l $10000040,d0
270             cmp.l #$feffeeff,d0
271             bne.s NoZ3
272             move.l d1,$10000040
273
274             move.l #$8000000,d0         ; Add some Z3 fast RAM if it hasn't been moved (Kick 1.3)
275             move.l #$405,d1
276             move.l #10,d2
277             move.l #$10000000,a0
278             move.l #0,a1
279             jsr AddMemList(a6)
280
281 NoZ3:
282             move.l  #11,PiSCSIDebugMe
283             lea LibName(pc),a1
284             jsr FindResident(a6)
285             move.l  #10,PiSCSIDebugMe
286             cmp.l #0,d0
287             bne.s SkipDriverLoad        ; Library is already loaded, jump straight to partitions
288
289             move.l  #4,PiSCSIDebugMe
290             movea.l 4,a6
291             move.l #$40000,d0
292             moveq #0,d1
293             jsr AllocMem(a6)            ; Allocate memory for the PiStorm to copy the driver to
294
295             move.l  d0,PiSCSIDriver     ; Copy the PiSCSI driver to allocated memory and patch offsets
296
297             move.l  #5,PiSCSIDebugMe
298             move.l  d0,a1
299             move.l  #0,d1
300             movea.l  4,a6
301             add.l #$02c,a1
302             jsr InitResident(a6)        ; Initialize the PiSCSI driver
303
304 SkipDriverLoad:
305             move.l  #9,PiSCSIDebugMe
306             bra.w LoadFileSystems
307
308 FSLoadExit:
309             lea ExpansionName(pc),a1
310             moveq #0,d0
311             jsr OpenLibrary(a6)         ; Open expansion.library to make this work, somehow
312             move.l a6,a4
313             move.l d0,a6
314
315             move.l  #7,PiSCSIDebugMe
316 PartitionLoop:
317             move.l PiSCSIGetPart,d0     ; Get the available partition in the current slot
318             beq.w EndPartitions         ; If the next partition returns 0, there's no additional partitions
319             move.l d0,a0
320             jsr MakeDosNode(a6)
321             move.l d0,PiSCSISetFSH
322             move.l d0,PiSCSIAddr2       ; Put DeviceNode address in PiSCSIAddr2, because I'm useless
323             move.l d0,a0
324             move.l PiSCSIGetPrio,d0
325             move.l #0,d1
326             move.l PiSCSIAddr1,a1
327
328 * Uncomment these lines to test AddDosNode/Enqueue stuff
329 * Or comment them out all the way down to and including SkipEnqueue: to use the AddBootNode method instead.
330             cmp.l   #-128,d0
331             bne.s   EnqueueNode
332
333 * BOOL AddDosNode( LONG bootPri, ULONG flags, struct DeviceNode *deviceNode );
334 * amicall(ExpansionBase, 0x96, AddDosNode(d0,d1,a0))
335             move.l #38,PiSCSIDebugMe
336             jsr AddDosNode(a6)
337             bra.w SkipEnqueue
338 * VOID Enqueue( struct List *list, struct Node *node );
339 * amicall(SysBase, 0x10e, Enqueue(a0,a1))
340
341 EnqueueNode:
342             exg a6,a4
343             move.l #35,PiSCSIDebugMe
344             move.l #BootNode_SIZEOF,PiSCSIDebugMe
345             move.l #NT_BOOTNODE,PiSCSIDebugMe
346             move.l #LN_TYPE,PiSCSIDebugMe
347             move.l #LN_PRI,PiSCSIDebugMe
348             move.l #LN_NAME,PiSCSIDebugMe
349             move.l #eb_MountList,PiSCSIDebugMe
350             move.l #35,PiSCSIDebugMe
351
352             move.l #BootNode_SIZEOF,d0
353             move.l #$10001,d1
354             jsr AllocMem(a6)            ; Allocate memory for the BootNode
355
356             move.l d0,PiSCSIAddr3
357             move.l #36,PiSCSIDebugMe
358
359             move.l d0,a1
360             move.b #NT_BOOTNODE,LN_TYPE(a1)
361             move.l PiSCSIGetPrio,d0
362             move.b d0,LN_PRI(a1)
363             move.l PiSCSIAddr2,bn_DeviceNode(a1)
364             move.l PiSCSIAddr1,LN_NAME(a1)
365
366             lea eb_MountList(a4),a0
367             jsr Enqueue(a6)
368             exg a6,a4
369
370 SkipEnqueue:
371
372 * BOOL AddBootNode( LONG bootPri, ULONG flags, struct DeviceNode *deviceNode, struct ConfigDev *configDev );
373 * amicall(ExpansionBase, 0x24, AddBootNode(d0,d1,a0,a1))
374 * Comment out the line below to test AddDosNode/Enqueue stuff
375 *            jsr AddBootNode(a6)
376             move.l #1,PiSCSINextPart    ; Switch to the next partition
377             bra.w PartitionLoop
378
379
380 EndPartitions:
381             move.l #8,PiSCSIDebugMe
382             move.l a6,a1
383             move.l #800,PiSCSIDebugMe
384             movea.l 4,a6
385             move.l #801,PiSCSIDebugMe
386             jsr CloseLibrary(a6)
387             move.l #802,PiSCSIDebugMe
388
389             move.l (a7)+,a6             ; Pop A6 from stack
390             move.l #803,PiSCSIDebugMe
391
392             move.w #$80B8,$dff09a       ; Re-enable interrupts
393             move.l #804,PiSCSIDebugMe
394             moveq.l #1,d0               ; indicate "success"
395             move.l #805,PiSCSIDebugMe
396             rts
397
398             align 4
399 FileSysName     dc.b    'FileSystem.resource',0
400 FileSysCreator  dc.b    'PiStorm',0
401
402 CurFS:          dc.l    $0
403 FSResource:     dc.l    $0
404
405             align 2
406 LoadFileSystems:
407             movem.l d0-d7/a0-a6,-(sp)       ; Push registers to stack
408             move.l #30,PiSCSIDebugMe
409 ReloadResource:
410             lea FileSysName(pc),a1
411             jsr OpenResource(a6)
412             tst.l d0
413             bne FSRExists
414
415             move.l #33,PiSCSIDebugMe        ; FileSystem.resource isn't open, create it
416                                             ; Code based on WinUAE filesys.asm
417
418             moveq #32,d0                    ; sizeof(FileSysResource)
419             move.l #$10001,d1
420             jsr AllocMem(a6)
421             move.l d0,a2
422             move.b #8,8(a2)                 ; NT_RESOURCE
423             lea FileSysName(pc),a0
424             move.l a0,10(a2)                ; node name
425             lea FileSysCreator(pc),a0
426             move.l a0,14(a2)                ; fsr_Creator
427             lea 18(a2),a0
428             move.l a0,(a0)                  ; NewList() fsr_FileSysEntries
429             addq.l #4,(a0)
430             move.l a0,8(a0)
431             lea $150(a6),a0                 ; ResourceList
432             move.l a2,a1
433             jsr -$f6(a6)                    ; AddTail
434             move.l a2,a0
435             bra.s ReloadResource
436
437 FSRExists:  
438             move.l d0,PiSCSIAddr2             ; PiSCSIAddr2 is now FileSystem.resource
439             move.l #31,PiSCSIDebugMe
440             move.l PiSCSIAddr2,a0
441             move.l PiSCSIGetFS,d0
442             cmp.l #0,d0
443             beq.w FSDone
444             move.l d0,d7
445
446 FSNext:     
447             move.l #45,PiSCSIDebugMe
448             lea fsr_FileSysEntries(a0),a0
449             move.l a0,d2
450             move.l LH_HEAD(a0),d0
451             beq.w NoEntries
452
453 FSLoop:     
454             move.l #34,PiSCSIDebugMe
455             move.l d0,a1
456             move.l #35,PiSCSIDebugMe
457             cmp.l fse_DosType(a1),d7
458             move.l #36,PiSCSIDebugMe
459             beq.w AlreadyLoaded
460             move.l #37,PiSCSIDebugMe
461             move.l LN_SUCC(a1),d0
462             bne.w FSLoop
463             move.l #390,PiSCSIDebugMe
464             bra.w NoEntries
465
466             align 2
467 NoEntries:  
468             move.l #39,PiSCSIDebugMe
469             move.l PiSCSIFSSize,d0
470             move.l #40,PiSCSIDebugMe
471             move.l #0,d1
472             move.l #41,PiSCSIDebugMe
473             jsr AllocMem(a6)
474             move.l d0,PiSCSIAddr3
475             move.l #1,PiSCSICopyFS
476
477 AlreadyLoaded:
478             move.l #480,PiSCSIDebugMe
479             move.l PiSCSIAddr2,a0
480             move.l #1,PiSCSINextFS
481             move.l PiSCSIGetFS,d0
482             move.l d0,d7
483             cmp.l #0,d0
484             bne.w FSNext
485
486 FSDone:     move.l #37,PiSCSIDebugMe
487             move.l #32,PiSCSIDebugMe    ; Couldn't open FileSystem.resource, Kick 1.2/1.3?
488
489             movem.l (sp)+,d0-d7/a0-a6   ; Pop registers from stack
490             bra.w FSLoadExit
491
492 FSRes
493     dc.l    0
494     dc.l    0
495     dc.b    NT_RESOURCE
496     dc.b    0
497     dc.l    FileSysName
498     dc.l    FileSysCreator
499 .Head
500     dc.l    .Tail
501 .Tail
502     dc.l    0
503     dc.l    .Head
504     dc.b    NT_RESOURCE
505     dc.b    0