3a36e090d951697342021f326b90fd4389070952
[swan-dev] / src / init.s
1
2 %include "WonderSwan.inc"
3
4 extern  _main
5 extern __end
6
7 MYSEGMENT       equ     0xF000
8
9 ; Set Maps and Sprite Table at the end of first part of ram
10 fgmap           equ     WSC_TILE_BANK1-MAP_SIZE
11 bgmap           equ     fgmap-MAP_SIZE
12 sprtable        equ     bgmap-SPR_TABLE_SIZE
13
14
15         cpu     186
16         bits    16
17
18         ;PADDING        15              ; Pad 15 Sections
19
20         section .text
21
22         ;ORG    0x0000
23
24 ..start:
25         cli                     ; Disable Interrupts
26         cld                     ; Offset in Increment mode
27
28         xor     ax,ax           ; Clear all registers
29         mov     bx,ax
30         mov     cx,ax
31         mov     dx,ax
32         mov     si,ax
33         mov     di,ax
34         mov     ds,ax           ; Clear Segments
35         mov     es,ax
36
37         mov     bp,ax           ; Setup stack
38         mov     ss,ax
39         mov     sp,WS_STACK
40
41         in      al,IO_HARDWARE_TYPE     ; Check Wonderswan Mono/Color
42         test    al,WS_COLOR 
43 .mono:  jz      .mono           ; We loop forever if Mono
44
45         mov     sp,WSC_STACK    ; New stack
46         
47         call    _main
48
49         xor     ax,ax           ; Clear Ram
50         mov     di,0x100
51         mov     cx,0x7E80
52         rep     stosw
53
54         out     IO_SRAM_BANK,al
55
56         ; Video Init
57         in      al,IO_VIDEO_MODE
58         or      al,VMODE_16C_CHK | VMODE_CLEANINIT
59         out     IO_VIDEO_MODE,al
60
61         xor     ax,ax
62
63         out     IO_BG_X,al
64         out     IO_BG_Y,al
65         out     IO_FG_X,al
66         out     IO_FG_Y,al
67
68         out     IO_SPR_START,al
69         out     IO_SPR_STOP,al
70
71         mov     al,BG_MAP(bgmap) | FG_MAP(fgmap)
72         out     IO_FGBG_MAP,al
73
74         mov     al,SPR_TABLE(sprtable)
75         out     IO_SPR_TABLE,al
76
77         in      al,IO_LCD_CTRL
78         or      al,LCD_ON
79         out     IO_LCD_CTRL,al
80
81         xor     al,al
82         out     IO_LCD_ICONS,al
83
84         ; Setup VBlank Int
85         mov     ax,INT_BASE
86         out     IO_INT_BASE,al
87
88         mov     di,INTVEC_VBLANK_START
89         add     di,ax
90         shl     di,2                    ; *4
91         mov     word [es:di],VBlank
92         mov     word [es:di+0x2],MYSEGMENT
93
94         xor     ax,ax                   ; Clear HBL & Timer
95         out     IOw_HBLANK_FREQ,ax
96         out     IO_TIMER_CTRL,al
97
98         dec     al                      ; Ack all Int
99         out     IO_INT_ACK,al
100
101         mov     al,INT_VBLANK_START     ; Enable VBL Int
102         out     IO_INT_ENABLE,al
103
104         sti                             ; Enable Interrupts !
105
106 ;--------------------------------
107
108         ; Set Segments for Copy
109         mov     ax,MYSEGMENT
110         mov     ds,ax
111         xor     ax,ax
112         mov     es,ax
113
114         ; Make BG
115
116         mov     si,TileGfx
117         mov     di,WSC_TILE_BANK1
118         mov     cx,(TileGfxE-TileGfx)/2
119         rep     movsw
120
121         mov     si,TilePal
122         mov     di,WSC_PALETTES
123         mov     cx,16
124         rep     movsw
125
126         mov     ax,BG_CHR(0,0,0,0,0)    ; Initial Tile, Pal 0, Bank 0
127         mov     di,bgmap
128         mov     cx,MAP_TWIDTH*MAP_THEIGHT
129         rep     stosw
130
131         ; Display !
132         mov     al,BG_ON | FG_ON
133         out     IO_DISPLAY_CTRL,al
134
135         ; Loop Read Buttons
136 loop:
137         mov     al,KEYPAD_READ_BUTTONS
138         out     IO_KEYPAD,al
139         nop
140         nop
141         nop
142         nop
143         in      al,IO_KEYPAD
144
145         test    al,PAD_A        ; Press A for Audio
146         jz      loop
147
148         call    _main
149         jmp     loop
150
151 ;******************************************
152
153 VBlank:
154         push    ax
155
156         mov     al,[es:VBLcnt]
157         inc     al
158         mov     [es:VBLcnt],al
159         and     al,3
160         jnz     .noscroll
161
162         in      al,IO_BG_X      ; Infinite Scroll
163         inc     al
164         out     IO_BG_X,al
165
166         in      al,IO_BG_Y
167         inc     al
168         out     IO_BG_Y,al
169
170 .noscroll:
171         in      al,IO_INT_ACK
172         or      al,INT_VBLANK_START
173         out     IO_INT_ACK,al
174
175         pop     ax
176         iret
177
178 ;******************************************
179
180         align   2
181
182 TilePal:        incbin  "gfx/bgtile.pal"
183 TileGfx:        incbin  "gfx/bgtile.gfx"
184 TileGfxE:
185
186 ;******************************************
187
188         ;ROM_HEADER     __end, MYSEGMENT, 0x42, RH_WS_COLOR, RH_ROM_8MBITS, RH_NO_SRAM, RH_HORIZONTAL
189
190 ;******************************************
191
192         ;section .bss   
193
194         ;start=0x0100   ; Keep space for Int Vectors
195
196 VBLcnt:         resb    0       ; Our datas in RAM
197