testpico: more on timer reload
[megadrive.git] / nshtest / sega_gcc.s
1 *-------------------------------------------------------\r
2 *\r
3 *       Sega startup code for the GNU Assembler\r
4 *       Translated from:\r
5 *       Sega startup code for the Sozobon C compiler\r
6 *       Written by Paul W. Lee\r
7 *       Modified from Charles Coty's code\r
8 *\r
9 *-------------------------------------------------------\r
10 \r
11         dc.l 0x0,0x200\r
12         dc.l INT,INT,INT,INT,INT,INT,INT\r
13         dc.l INT,INT,INT,INT,INT,INT,INT,INT\r
14         dc.l INT,INT,INT,INT,INT,INT,INT,INT\r
15         dc.l INT,INT,INT,HBL,INT,VBL,INT,INT\r
16         dc.l INT,INT,INT,INT,INT,INT,INT,INT\r
17         dc.l INT,INT,INT,INT,INT,INT,INT,INT\r
18         dc.l INT,INT,INT,INT,INT,INT,INT,INT\r
19         dc.l INT,INT,INT,INT,INT,INT,INT\r
20         .ascii "SEGA GENESIS                    "\r
21         .ascii "notaz's Shadow / Hilight test                   "\r
22         .ascii "NOTAZ'S SHADOW HILIGHT TEST                     "\r
23         .ascii "GM 00000000-00"\r
24         .byte 0xa5,0xfb\r
25         .ascii "JD              "\r
26         .byte 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00\r
27         .byte 0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff\r
28         .ascii "               "\r
29         .ascii "                        "\r
30         .ascii "                         "\r
31         .ascii "JUE             "\r
32 *debugee:\r
33 *        bra     debugee\r
34         tst.l   0xa10008\r
35         bne     SkipJoyDetect                               \r
36         tst.w   0xa1000c\r
37 SkipJoyDetect:\r
38         bne     SkipSetup\r
39         lea     Table,%a5                       \r
40         movem.w (%a5)+,%d5-%d7\r
41         movem.l (%a5)+,%a0-%a4                       \r
42 * Check Version Number                      \r
43         move.b  -0x10ff(%a1),%d0\r
44         andi.b  #0x0f,%d0                             \r
45         beq     WrongVersion                                   \r
46 * Sega Security Code (SEGA)   \r
47         move.l  #0x53454741,0x2f00(%a1)\r
48 WrongVersion:\r
49         move.w  (%a4),%d0\r
50         moveq   #0x00,%d0                                \r
51         movea.l %d0,%a6                                  \r
52         move    %a6,%usp\r
53 * Set VDP registers\r
54         moveq   #0x17,%d1\r
55 FillLoop:                           \r
56         move.b  (%a5)+,%d5\r
57         move.w  %d5,(%a4)                              \r
58         add.w   %d7,%d5                                 \r
59         dbra    %d1,FillLoop                           \r
60         move.l  (%a5)+,(%a4)                            \r
61         move.w  %d0,(%a3)                                 \r
62         move.w  %d7,(%a1)                                 \r
63         move.w  %d7,(%a2)                                 \r
64 L0250:\r
65         btst    %d0,(%a1)\r
66         bne     L0250                                   \r
67 * Put initial values into a00000                \r
68         moveq   #0x25,%d2\r
69 Filla:                                 \r
70         move.b  (%a5)+,(%a0)+\r
71         dbra    %d2,Filla\r
72         move.w  %d0,(%a2)                                 \r
73         move.w  %d0,(%a1)                                 \r
74         move.w  %d7,(%a2)                                 \r
75 L0262:\r
76         move.l  %d0,-(%a6)\r
77         dbra    %d6,L0262                            \r
78         move.l  (%a5)+,(%a4)                              \r
79         move.l  (%a5)+,(%a4)                              \r
80 * Put initial values into c00000                  \r
81         moveq   #0x1f,%d3\r
82 Filc0:                             \r
83         move.l  %d0,(%a3)\r
84         dbra    %d3,Filc0\r
85         move.l  (%a5)+,(%a4)                              \r
86 * Put initial values into c00000                 \r
87         moveq   #0x13,%d4\r
88 Fillc1:                            \r
89         move.l  %d0,(%a3)\r
90         dbra    %d4,Fillc1\r
91 * Put initial values into c00011                 \r
92         moveq   #0x03,%d5\r
93 Fillc2:                            \r
94         move.b  (%a5)+,0x0011(%a3)        \r
95         dbra    %d5,Fillc2                            \r
96         move.w  %d0,(%a2)                                 \r
97         movem.l (%a6),%d0-%d7/%a0-%a6                    \r
98         move    #0x2700,%sr                           \r
99 SkipSetup:\r
100         bra     Continue\r
101 Table:\r
102         dc.w    0x8000, 0x3fff, 0x0100, 0x00a0, 0x0000, 0x00a1, 0x1100, 0x00a1\r
103         dc.w    0x1200, 0x00c0, 0x0000, 0x00c0, 0x0004, 0x0414, 0x302c, 0x0754\r
104         dc.w    0x0000, 0x0000, 0x0000, 0x812b, 0x0001, 0x0100, 0x00ff, 0xff00                                   \r
105         dc.w    0x0080, 0x4000, 0x0080, 0xaf01, 0xd91f, 0x1127, 0x0021, 0x2600\r
106         dc.w    0xf977, 0xedb0, 0xdde1, 0xfde1, 0xed47, 0xed4f, 0xd1e1, 0xf108                                   \r
107         dc.w    0xd9c1, 0xd1e1, 0xf1f9, 0xf3ed, 0x5636, 0xe9e9, 0x8104, 0x8f01                \r
108         dc.w    0xc000, 0x0000, 0x4000, 0x0010, 0x9fbf, 0xdfff                                \r
109 \r
110 Continue:\r
111         tst.w    0x00C00004\r
112 \r
113 * set stack pointer\r
114 *        clr.l   %a7\r
115         move.w   #0,%a7\r
116 \r
117 * user mode\r
118         move.w  #0x2300,%sr\r
119 \r
120 * clear Genesis RAM\r
121         lea     0xff0000,%a0\r
122         moveq   #0,%d0\r
123 clrram: move.w  #0,(%a0)+\r
124         subq.w  #2,%d0\r
125         bne     clrram\r
126 \r
127 *----------------------------------------------------------        \r
128 *\r
129 *       Load driver into the Z80 memory\r
130 *\r
131 *----------------------------------------------------------        \r
132 \r
133 * halt the Z80\r
134         move.w  #0x100,0xa11100\r
135 * reset it\r
136         move.w  #0x100,0xa11200\r
137 \r
138         lea     Z80Driver,%a0\r
139         lea     0xa00000,%a1\r
140         move.l  #Z80DriverEnd,%d0\r
141         move.l  #Z80Driver,%d1\r
142         sub.l   %d1,%d0\r
143 Z80loop:\r
144         move.b  (%a0)+,(%a1)+\r
145         subq.w  #1,%d0\r
146         bne     Z80loop\r
147 \r
148 * enable the Z80\r
149         move.w  #0x0,0xa11100\r
150 \r
151 *----------------------------------------------------------        \r
152         jmp      main\r
153 \r
154 INT:    \r
155         rte\r
156 \r
157 HBL:\r
158         /* addq.l   #1,htimer */\r
159         rte\r
160 \r
161 VBL:\r
162         /* addq.l   #1,vtimer */\r
163         move.l #vtimer,a0\r
164         addq.l #1,(a0)\r
165         rte\r
166 \r
167 *------------------------------------------------\r
168 *\r
169 *       Get a random number.  This routine\r
170 *       was found in TOS.\r
171 *\r
172 *       Output\r
173 *       ------\r
174 *       d0 = random number\r
175 *\r
176 *------------------------------------------------\r
177 \r
178         .globl  random\r
179 \r
180 random:\r
181                 move.l      rand_num,%d0\r
182                 tst.l       %d0\r
183                 bne         .L1\r
184                 moveq       #16,%d1\r
185                 lsl.l       %d1,%d0\r
186                 or.l        htimer,%d0\r
187                 move.l      %d0,rand_num\r
188 .L1:\r
189                 move.l      #-1153374675,-(%sp)\r
190                 move.l      rand_num,-(%sp)\r
191                 bsr         lmul\r
192                 addq.w      #8,%sp\r
193                 addq.l      #1,%d0\r
194                 move.l      %d0,rand_num\r
195 \r
196                 lsr.l       #8,%d0\r
197                 and.l       #16777215,%d0\r
198                 rts\r
199 \r
200 \r
201 *------------------------------------------------\r
202 *\r
203 * Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg\r
204 *\r
205 * Permission is granted to anyone to use this software for any purpose\r
206 * on any computer system, and to redistribute it freely, with the\r
207 * following restrictions:\r
208 * 1) No charge may be made other than reasonable charges for reproduction.\r
209 * 2) Modified versions must be clearly marked as such.\r
210 * 3) The authors are not responsible for any harmful consequences\r
211 *    of using this software, even if they result from defects in it.\r
212 *\r
213 *------------------------------------------------\r
214 \r
215 ldiv:\r
216         move.l  4(%a7),%d0\r
217         bpl     ld1\r
218         neg.l   %d0\r
219 ld1:\r
220         move.l  8(%a7),%d1\r
221         bpl     ld2\r
222         neg.l   %d1\r
223         eor.b   #0x80,4(%a7)\r
224 ld2:\r
225         bsr     i_ldiv          /* d0 = d0/d1 */\r
226         tst.b   4(%a7)\r
227         bpl     ld3\r
228         neg.l   %d0\r
229 ld3:\r
230         rts\r
231 \r
232 lmul:\r
233         move.l  4(%a7),%d0\r
234         bpl     lm1\r
235         neg.l   %d0\r
236 lm1:\r
237         move.l  8(%a7),%d1\r
238         bpl     lm2\r
239         neg.l   %d1\r
240         eor.b   #0x80,4(%a7)\r
241 lm2:\r
242         bsr     i_lmul          /* d0 = d0*d1 */\r
243         tst.b   4(%a7)\r
244         bpl     lm3\r
245         neg.l   %d0\r
246 lm3:\r
247         rts\r
248 \r
249 lrem:\r
250         move.l  4(%a7),%d0\r
251         bpl     lr1\r
252         neg.l   %d0\r
253 lr1:\r
254         move.l  8(%a7),%d1\r
255         bpl     lr2\r
256         neg.l   %d1\r
257 lr2:\r
258         bsr     i_ldiv          /* d1 = d0%d1 */\r
259         move.l  %d1,%d0\r
260         tst.b   4(%a7)\r
261         bpl     lr3\r
262         neg.l   %d0\r
263 lr3:\r
264         rts\r
265 \r
266 ldivu:\r
267         move.l  4(%a7),%d0\r
268         move.l  8(%a7),%d1\r
269         bsr     i_ldiv\r
270         rts\r
271 \r
272 lmulu:\r
273         move.l  4(%a7),%d0\r
274         move.l  8(%a7),%d1\r
275         bsr     i_lmul\r
276         rts\r
277 \r
278 lremu:\r
279         move.l  4(%a7),%d0\r
280         move.l  8(%a7),%d1\r
281         bsr     i_ldiv\r
282         move.l  %d1,%d0\r
283         rts\r
284 *\r
285 * A in d0, B in d1, return A*B in d0\r
286 *\r
287 i_lmul:\r
288         move.l  %d3,%a2           /* save d3 */\r
289         move.w  %d1,%d2\r
290         mulu    %d0,%d2           /* d2 = Al * Bl */\r
291 \r
292         move.l  %d1,%d3\r
293         swap    %d3\r
294         mulu    %d0,%d3           /* d3 = Al * Bh */\r
295 \r
296         swap    %d0\r
297         mulu    %d1,%d0           /* d0 = Ah * Bl */\r
298 \r
299         add.l   %d3,%d0           /* d0 = (Ah*Bl + Al*Bh) */\r
300         swap    %d0\r
301         clr.w   %d0              /* d0 = (Ah*Bl + Al*Bh) << 16 */\r
302 \r
303         add.l   %d2,%d0           /* d0 = A*B */\r
304         move.l  %a2,%d3           /* restore d3 */\r
305         rts\r
306 *\r
307 *A in d0, B in d1, return A/B in d0, A%B in d1\r
308 *\r
309 i_ldiv:\r
310         tst.l   %d1\r
311         bne     nz1\r
312 \r
313 *       divide by zero\r
314 *       divu    #0,%d0           /* cause trap */\r
315         move.l  #0x80000000,%d0\r
316         move.l  %d0,%d1\r
317         rts\r
318 nz1:\r
319         move.l  %d3,%a2           /* save d3 */\r
320         cmp.l   %d1,%d0\r
321         bhi     norm\r
322         beq     is1\r
323 *       A<B, so ret 0, rem A\r
324         move.l  %d0,%d1\r
325         clr.l   %d0\r
326         move.l  %a2,%d3           /* restore d3 */\r
327         rts\r
328 *       A==B, so ret 1, rem 0\r
329 is1:\r
330         moveq.l #1,%d0\r
331         clr.l   %d1\r
332         move.l  %a2,%d3           /* restore d3 */\r
333         rts\r
334 *       A>B and B is not 0\r
335 norm:\r
336         cmp.l   #1,%d1\r
337         bne     not1\r
338 *       B==1, so ret A, rem 0\r
339         clr.l   %d1\r
340         move.l  %a2,%d3           /* restore d3 */\r
341         rts\r
342 *  check for A short (implies B short also)\r
343 not1:\r
344         cmp.l   #0xffff,%d0\r
345         bhi     slow\r
346 *  A short and B short -- use 'divu'\r
347         divu    %d1,%d0           /* d0 = REM:ANS */\r
348         swap    %d0              /* d0 = ANS:REM */\r
349         clr.l   %d1\r
350         move.w  %d0,%d1           /* d1 = REM */\r
351         clr.w   %d0\r
352         swap    %d0\r
353         move.l  %a2,%d3           /* restore d3 */\r
354         rts\r
355 * check for B short\r
356 slow:\r
357         cmp.l   #0xffff,%d1\r
358         bhi     slower\r
359 * A long and B short -- use special stuff from gnu\r
360         move.l  %d0,%d2\r
361         clr.w   %d2\r
362         swap    %d2\r
363         divu    %d1,%d2           /* d2 = REM:ANS of Ahi/B */\r
364         clr.l   %d3\r
365         move.w  %d2,%d3           /* d3 = Ahi/B */\r
366         swap    %d3\r
367 \r
368         move.w  %d0,%d2           /* d2 = REM << 16 + Alo */\r
369         divu    %d1,%d2           /* d2 = REM:ANS of stuff/B */\r
370 \r
371         move.l  %d2,%d1\r
372         clr.w   %d1\r
373         swap    %d1              /* d1 = REM */\r
374 \r
375         clr.l   %d0\r
376         move.w  %d2,%d0\r
377         add.l   %d3,%d0           /* d0 = ANS */\r
378         move.l  %a2,%d3           /* restore d3 */\r
379         rts\r
380 *       A>B, B > 1\r
381 slower:\r
382         move.l  #1,%d2\r
383         clr.l   %d3\r
384 moreadj:\r
385         cmp.l   %d0,%d1\r
386         bhs     adj\r
387         add.l   %d2,%d2\r
388         add.l   %d1,%d1\r
389         bpl     moreadj\r
390 * we shifted B until its >A or sign bit set\r
391 * we shifted #1 (d2) along with it\r
392 adj:\r
393         cmp.l   %d0,%d1\r
394         bhi     ltuns\r
395         or.l    %d2,%d3\r
396         sub.l   %d1,%d0\r
397 ltuns:\r
398         lsr.l   #1,%d1\r
399         lsr.l   #1,%d2\r
400         bne     adj\r
401 * d3=answer, d0=rem\r
402         move.l  %d0,%d1\r
403         move.l  %d3,%d0\r
404         move.l  %a2,%d3           /* restore d3 */\r
405         rts\r
406 *----------------------------------------------------------        \r
407 *\r
408 *       Z80 Sound Driver\r
409 *\r
410 *----------------------------------------------------------        \r
411 Z80Driver:\r
412           dc.b  0xc3,0x46,0x00,0x00,0x00,0x00,0x00,0x00\r
413           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
414           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
415           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
416           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
417           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
418           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
419           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00\r
420           dc.b  0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0xed\r
421           dc.b  0x56,0x31,0x00,0x20,0x3a,0x39,0x00,0xb7\r
422           dc.b  0xca,0x4c,0x00,0x21,0x3a,0x00,0x11,0x40\r
423           dc.b  0x00,0x01,0x06,0x00,0xed,0xb0,0x3e,0x00\r
424           dc.b  0x32,0x39,0x00,0x3e,0xb4,0x32,0x02,0x40\r
425           dc.b  0x3e,0xc0,0x32,0x03,0x40,0x3e,0x2b,0x32\r
426           dc.b  0x00,0x40,0x3e,0x80,0x32,0x01,0x40,0x3a\r
427           dc.b  0x43,0x00,0x4f,0x3a,0x44,0x00,0x47,0x3e\r
428           dc.b  0x06,0x3d,0xc2,0x81,0x00,0x21,0x00,0x60\r
429           dc.b  0x3a,0x41,0x00,0x07,0x77,0x3a,0x42,0x00\r
430           dc.b  0x77,0x0f,0x77,0x0f,0x77,0x0f,0x77,0x0f\r
431           dc.b  0x77,0x0f,0x77,0x0f,0x77,0x0f,0x77,0x3a\r
432           dc.b  0x40,0x00,0x6f,0x3a,0x41,0x00,0xf6,0x80\r
433           dc.b  0x67,0x3e,0x2a,0x32,0x00,0x40,0x7e,0x32\r
434           dc.b  0x01,0x40,0x21,0x40,0x00,0x7e,0xc6,0x01\r
435           dc.b  0x77,0x23,0x7e,0xce,0x00,0x77,0x23,0x7e\r
436           dc.b  0xce,0x00,0x77,0x3a,0x39,0x00,0xb7,0xc2\r
437           dc.b  0x4c,0x00,0x0b,0x78,0xb1,0xc2,0x7f,0x00\r
438           dc.b  0x3a,0x45,0x00,0xb7,0xca,0x4c,0x00,0x3d\r
439           dc.b  0x3a,0x45,0x00,0x06,0xff,0x0e,0xff,0xc3\r
440           dc.b  0x7f,0x00\r
441 Z80DriverEnd:\r
442 \r
443 \r