1 ###############################################################################
3 # Copyright (c) 2009,2011 GraÅžvydas Ignotas
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the organization nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # --register-prefix-optional --bitwise-or
34 .equ RELOCATE_TO_RAM, 1
42 ##################################################
44 # Register and bitmask definitions #
46 ##################################################
48 .equ GFXDATA, 0xc00000
49 .equ GFXCNTL, 0xc00004
52 .equ VDP0_E_DISPLAY, 0x02
53 .equ VDP0_PLTT_FULL, 0x04
55 .equ VDP1_SMS_MODE, 0x80
56 .equ VDP1_E_DISPLAY, 0x40
64 .equ VDP12_SCREEN_V224, 0x00
65 .equ VDP12_SCREEN_V448, 0x04
66 .equ VDP12_PROGRESSIVE, 0x00
67 .equ VDP12_INTERLACED, 0x02
68 .equ VDP12_SCREEN_H256, 0x00
69 .equ VDP12_SCREEN_H320, 0x81
71 .equ VDP16_MAP_V32, 0x00
72 .equ VDP16_MAP_V64, 0x10
73 .equ VDP16_MAP_V128, 0x30
74 .equ VDP16_MAP_H32, 0x00
75 .equ VDP16_MAP_H64, 0x01
76 .equ VDP16_MAP_H128, 0x03
79 .equ MMODE_VAL_INPUT, 1
80 .equ MMODE_EDIT_VAL, 2
82 .equ MMODE_START_MENU, 4
83 .equ MMODE_GOTO_PREDEF, 5
84 .equ MMODE_JMP_ADDR, 6
87 .equ predef_addr_cnt, ((predef_addrs_end-predef_addrs)/4)
89 ##################################################
93 ##################################################
96 # Write val to VDP register reg
97 .macro write_vdp_r_dst reg val dst
98 move.w #((\reg << 8) + 0x8000 + \val),\dst
101 # Write val to VDP register reg, vdp addr in a3
102 .macro write_vdp_reg reg val
103 write_vdp_r_dst \reg, \val, (a3)
106 # Set up address in VDP, control port in dst
107 .macro VRAM_ADDR adr dst
108 move.l #(0x40000000 | ((\adr & 0x3fff) << 16) | (\adr >> 14)),\dst
111 .macro VSRAM_ADDR adr dst
112 move.l #(0x40000010 | ((\adr & 0x3fff) << 16) | (\adr >> 14)),\dst
116 # make VDP word from address adr and store in d0
117 .macro XRAM_ADDR_var adr
127 .macro VRAM_ADDR_var adr
133 .macro CRAM_ADDR_var adr
139 # convert tile coords in d0, d1 to nametable addr to a0
148 # check if some d-pad button (and modifier) is pressed
149 .macro do_dpad bit op val
157 # convert a6 to normal addr
159 .macro mk_a6_addr reg
167 .macro change_mode mode_new mode_back
169 or.w #(\mode_back<<11)|(\mode_new<<8),d7
173 .macro menu_text str x y pal
177 move.l #0x8000|(\pal<<13),d2
181 #################################################
185 #################################################
188 dc.w 0x0000,0x0eee,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
189 dc.w 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
190 dc.w 0x0000,0x02e2,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
191 dc.w 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
192 dc.w 0x0000,0x0e44,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
193 dc.w 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
194 dc.w 0x0000,0x044e,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
195 dc.w 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
200 /* Y size link attr X */
201 dc.w 0; dc.b 0x05; dc.b 0; dc.w 0x6002; dc.w 0
205 dc.l 0x000000, 0x200000, 0x400000, 0xa00000, 0xa10000
206 dc.l 0xa11100, 0xa12000, 0xa13000, 0xa14000, 0xc00000
210 dc.l 0x000000, 0x7fffff
211 dc.l 0xe00000, 0xffffff
212 dc.l 0xa00000, 0xa100ff
213 dc.l 0xa11000, 0xa113ff
214 dc.l 0xa12000, 0xa120ff
215 dc.l 0xa13000, 0xa130ff
228 .ascii "Go to address\0"
230 .ascii "Go to (predef)\0"
232 .ascii "Jump to address\0"
234 .ascii "PC Transfer\0"
236 .ascii "DTACK safety\0"
238 .ascii "Transfer Ready\0"
242 .ascii "DTACK err?\0"
244 .ascii "Exception \0"
246 ##################################################
250 ##################################################
253 # a6 = page_start[31:8]|cursor_offs[7:0]
254 # d7 = old_inputs[31:16]|edit_bytes[15:14]|g_mode_old[13:11]|g_mode[10:8]|irq_cnt[7:0]
255 # d6 = edit_word_save[31:15]|edit_done[7]|no_dtack_detect[4]|autorep_cnt[3:0]
257 # edit: edit_word[31:8]|edit_pos[4:2]|byte_cnt[1:0]
263 /* make sure io port 2 is doing inputs */
264 move.b #0,(0xa1000b).l
265 /* make sure irqs are masked */
267 /* take care of TMSS */
268 move.b (0xa10000).l,d0
271 move.l #0x53454741,(0xa14000).l
272 /* want cart, not OS rom if cart pops in */
273 move.w #1,(0xa14100).l
274 /* touch VDP after TMSS setup? */
278 /* want to do early PC transfer (with RAM/VRAM intact and such)?
279 * also give time PC to see start condition */
285 bne no_early_transfer
287 move.b #0x40,(0xa1000b).l /* port 2 ctrl */
288 move.b #0x00,(a0) /* port 2 data - start with TH low */
292 beq do_early_transfer
296 move.b #0,(0xa1000b).l
297 bra no_early_transfer /* timeout */
306 /* copy to expansion device if magic number is set */
312 move.l (sizeof_bin,pc),d0
322 /* we could be relocated by 32x or something else, adjust start addr */
328 /* copy, assume 8K size */
330 move.l (sizeof_bin,pc),d0
338 lea (test_code,pc),a0
340 move.w #(test_code_end - test_code)/2-1,d0
351 /* patch test code */
353 add.w #(test_code_ret_op-test_code+2),a1
365 move.b #0x40,(0xa10009).l
366 move.b #0x40,(0xa10003).l
371 /* Clear h/v scroll */
373 VRAM_ADDR 0x8000,(GFXCNTL)
375 VSRAM_ADDR 0,(GFXCNTL)
378 /* Load color data */
381 moveq.l #(colors_end-colors)/2,d0
384 /* load font patterns */
387 VRAM_ADDR 0,(GFXCNTL)
399 rol.l #1,d1 /* fixup */
403 /* generate A layer map */
411 0: move.l #0x00000000,(a0)
417 /* generate B layer map */
422 0: move.l #0x00000000,(a0)
425 /* upload sprite data */
428 lea (sprite_data,pc),a1
430 move.l #(sprite_data_end-sprite_data)/2-1,d3
435 /* wait for vsync before unmask */
438 /* wait a bit to avoid nested vint */
441 dbra d0,0b /* 10 cycles to go back */
443 /* enable and unmask vint */
444 write_vdp_r_dst 1,(VDP1_E_VBI | VDP1_E_DISPLAY | VDP1_MODE5),(GFXCNTL)
448 ##################################################
461 /* let's hope VRAM is already set up.. */
469 ##################################################
473 # movem.l d0-d4/a0-a5,-(a7)
475 btst.b #5,(0xa10005).l
477 change_mode MMODE_PC, MMODE_MAIN
478 write_vdp_r_dst 12,(VDP12_SCREEN_V224 | VDP12_SCREEN_H320 | VDP12_STE),(GFXCNTL)
485 lea (jumptab,pc,d0),a0
488 /* branch insns here because we want to be position independent */
491 bra mode_edit_val /* edit val in editor */
498 ##################### main #######################
501 /* assume we will hang */
502 lea (txt_dtack_err,pc),a0
512 move.l d0,a1 /* current addr */
515 add.b #27-1,d1 /* line where the cursor sits */
519 move.l #27-1,d5 /* line counter for dbra */
542 # unsafe or partially safe
545 swap d4 /* mask in upper word */
547 bne draw_row_hsafe_words_pre
549 draw_row_hsafe_bytes_pre:
554 draw_row_hsafe_bytes:
560 move.l #'?'|('?'<<16),(a0)
562 move.l #'?'|('?'<<16),(a0)
572 dbra d4,draw_row_hsafe_bytes
579 beq draw_cursor_unsafe_byte
580 bra draw_chars_hsafe_pre
582 draw_row_hsafe_words_pre:
587 draw_row_hsafe_words:
593 move.l #'?'|('?'<<16),(a0)
594 move.l #'?'|('?'<<16),(a0)
601 dbra d4,draw_row_hsafe_words
603 move.l #(' '<<16)|' ',(a0)
608 beq draw_cursor_unsafe_word
610 draw_chars_hsafe_pre:
620 move.l #'?'|('?'<<16),(a0)
621 bra draw_chars_hsafe_next
623 btst.l #15,d7 /* must perform correct read type */
624 bne 0f /* doing byte reads on security reg hangs */
651 draw_chars_hsafe_next:
653 dbra d4,draw_chars_hsafe
655 move.l #(' '<<16)|' ',(a0)
694 move.l #(' '<<16)|' ',(a0)
716 move.l #(' '<<16)|' ',(a0)
738 move.l #' '|(' '<<16),(a0)
739 move.l #' '|(' '<<16),(a0)
744 jsr get_input /* x0cbrldu x1sa00du */
746 btst.l #16+4,d0 /* A - scroll modifier */
749 do_dpad 16+0, sub, #0x0800
750 do_dpad 16+1, add, #0x0800
751 do_dpad 16+10, sub, #0xd800
752 do_dpad 16+11, add, #0xd800
759 do_dpad 0, subq, #0x0008
760 do_dpad 1, addq, #0x0008
781 btst.l #12,d0 /* B - switch byte/word mode */
784 add.w #0x4000,d7 /* changes between 01 10 */
787 sub.l d1,a6 /* make even, just in case */
790 btst.l #13,d0 /* C - edit selected byte */
793 change_mode MMODE_EDIT_VAL, MMODE_MAIN
794 write_vdp_r_dst 12,(VDP12_SCREEN_V224 | VDP12_SCREEN_H320 | VDP12_STE),(GFXCNTL)
797 btst.l #5,d0 /* Start - menu */
801 change_mode MMODE_START_MENU, MMODE_MAIN
802 write_vdp_r_dst 12,(VDP12_SCREEN_V224 | VDP12_SCREEN_H320 | VDP12_STE),(GFXCNTL)
806 # movem.l (a7)+,d0-d4/a0-a5
814 draw_cursor_unsafe_byte:
816 and.l #7,d0 /* byte offs */
819 add.b d1,d0 /* d0 *= 3 (chars) */
823 move.l #(0x2000|'?'|((0x2000|'?')<<16)),(a0)
827 jsr load_prepare /* restore a0 */
828 bra draw_chars_hsafe_pre
830 draw_cursor_unsafe_word:
832 and.l #7,d0 /* byte offs */
834 lsr.b #1,d1 /* which word */
837 add.b d2,d1 /* num of chars to skip */
842 move.l #(0x2000|'?'|((0x2000|'?')<<16)),d0
848 jsr load_prepare /* restore a0 */
849 bra draw_chars_hsafe_pre
854 and.l #7,d0 /* byte offs */
860 add.b d1,d0 /* d0 *= 3 (chars) */
868 jsr load_prepare /* restore a0 */
874 and.l #7,d0 /* byte offs */
876 lsr.b #1,d1 /* which word */
879 add.b d2,d1 /* num of chars to skip */
890 jsr load_prepare /* restore a0 */
895 #################### hedit #######################
899 bne mode_hedit_finish
901 /* read val to edit */
917 change_mode MMODE_VAL_INPUT, MMODE_EDIT_VAL
936 ##################### goto #######################
947 or.b #3,d5 /* 3 bytes */
949 change_mode MMODE_VAL_INPUT, MMODE_GOTO
969 ################### val edit #####################
973 movea.l #0xe000+14*2+11*64*2,a1
993 lea (txt_a_confirm,pc),a0
1004 and.b #3,d3 /* edit field bytes */
1022 lsr.b #1,d1 /* length in bytes */
1026 and.b #7,d1 /* nibble to edit */
1030 sub.b #1,d3 /* chars to shift out */
1041 jsr get_input /* x0cbrldu x1sa00du */
1048 add.b d1,d1 /* nibble count */
1049 sub.b #1,d1 /* max n.t.e. val */
1052 and.b #7,d2 /* nibble to edit */
1064 lsl.l d1,d3 /* mask */
1065 lsl.l d1,d4 /* what to add/sub */
1076 eor.l #0xffffffff,d3
1110 btst.l #4,d0 /* A - confirm */
1113 move.w d7,d1 /* back to prev mode */
1123 ################### start menu ###################
1130 menu_text txt_about, 13, 9, 1
1131 menu_text txt_goto, 13, 11, 0
1132 menu_text txt_goto_predef, 13, 12, 0
1133 menu_text txt_jmp_addr, 13, 13, 0
1134 menu_text txt_dump, 13, 14, 0
1135 menu_text txt_dtack, 13, 15, 0
1136 menu_text txt_a_confirm, 13, 17, 2
1138 /* dtack safety on/off */
1139 movea.l #0xe000+26*2+15*64*2,a0
1141 move.w #0x8000|'O',(a0)
1144 move.w #0x8000|'N',(a0)
1147 move.w #0x8000|'F',(a0)
1148 move.w #0x8000|'F',(a0)
1152 movea.l #0xe000+11*2+11*64*2,a0
1162 jsr get_input /* x0cbrldu x1sa00du */
1171 or.b #1,d2 /* up -1, down 1 */
1186 btst.l #4,d0 /* A - confirm */
1191 change_mode MMODE_GOTO, MMODE_MAIN
1198 change_mode MMODE_GOTO_PREDEF, MMODE_MAIN
1204 change_mode MMODE_JMP_ADDR, MMODE_MAIN
1210 change_mode MMODE_PC, MMODE_MAIN
1230 movea.l #0xe000+10*2+8*64*2,a1
1244 ################### goto predef ##################
1248 movea.l #0xe000+14*2+8*64*2,a1
1249 move.l #predef_addr_cnt+2-1,d1
1261 /* draw addresses */
1262 movea.l #0xe000+17*2+9*64*2,a1
1263 lea (predef_addrs,pc),a2
1264 move.w #predef_addr_cnt-1,d4
1270 jsr print_hex_preped
1275 movea.l #0xe000+15*2+9*64*2,a0
1284 jsr get_input /* x0cbrldu x1sa00du */
1291 or.b #1,d2 /* up -1, down 1 */
1295 move.b #predef_addr_cnt-1,d5
1297 cmp.b #predef_addr_cnt-1,d5
1304 btst.l #4,d0 /* A - confirm */
1309 lea (predef_addrs,pc),a0
1312 bra mode_goto_finish
1323 ##################### jmp ########################
1330 or.b #3,d5 /* 3 bytes */
1332 change_mode MMODE_VAL_INPUT, MMODE_JMP_ADDR
1337 write_vdp_r_dst 1,(VDP1_E_DISPLAY | VDP1_MODE5),(GFXCNTL) /* disable vint */
1342 move.b #0x40,(0xa1000b).l /* port 2 ctrl */
1343 move.b #0x00,(0xa10005).l /* port 2 data - start with TH low */
1345 lea (txt_transfer_ready,pc),a0
1352 move.b (0xa10005),d0
1356 menu_text txt_working, 13, 13, 0
1360 # go back to main mode
1362 bclr.l #7,d6 /* not edited */
1363 change_mode MMODE_MAIN, MMODE_MAIN
1364 write_vdp_r_dst 12,(VDP12_SCREEN_V224 | VDP12_SCREEN_H320),(GFXCNTL)
1368 #################################################
1370 # Initialize VDP registers #
1372 #################################################
1376 write_vdp_reg 0,(VDP0_E_DISPLAY | VDP0_PLTT_FULL)
1377 write_vdp_reg 1,(VDP1_E_DISPLAY | VDP1_MODE5)
1378 write_vdp_reg 2,(0xe000 >> 10) /* Screen map a adress */
1379 write_vdp_reg 3,(0xe000 >> 10) /* Window address */
1380 write_vdp_reg 4,(0xc000 >> 13) /* Screen map b address */
1381 write_vdp_reg 5,(0xfc00 >> 9) /* Sprite address */
1383 write_vdp_reg 7,0 /* Backdrop color */
1384 write_vdp_reg 0x0a,1 /* Lines per hblank interrupt */
1385 write_vdp_reg 0x0b,0 /* 2-cell vertical scrolling */
1386 write_vdp_reg 0x0c,(VDP12_SCREEN_V224 | VDP12_SCREEN_H320)
1387 write_vdp_reg 0x0d,(0x8000 >> 10) /* Horizontal scroll address */
1388 write_vdp_reg 0x0e,0
1389 write_vdp_reg 0x0f,2
1390 write_vdp_reg 0x10,(VDP16_MAP_V32 | VDP16_MAP_H64) /* layer size */
1391 write_vdp_reg 0x11,0
1392 write_vdp_reg 0x12,0
1396 # get mask of bits representing safe words
1397 # a1 - address to check
1398 # destroys d0-d2, strips upper bits from a1
1403 lea (safe_addrs,pc),a1
1404 move.w #(safe_addrs_end - safe_addrs)/8-1,d2
1426 /* check for VDP address */
1431 bne addr_unsafe /* not vdp */
1440 blt addr_hsafe_3 /* data port */
1442 blt addr_safe /* below PSG */
1444 bge addr_safe /* above PSG */
1447 moveq.l #0,d0 /* skip line */
1451 moveq.l #3,d0 /* skip 2 words */
1461 # read single phase from controller
1466 move.b #0x40,(0xa10003)
1470 move.b (0xa10003),d0
1471 move.b #0x00,(0xa10003)
1474 move.b (0xa10003),d0
1479 eor.w d0,d1 /* changed btns */
1480 move.w d0,d7 /* old val */
1482 and.w d0,d1 /* what changed now */
1487 and.b #0x0f,d2 /* do autorepeat */
1498 # Prepare to write to VDP RAM @a0
1499 # sets a0 to VDP data port for convenience
1505 move.l d0,(GFXCNTL).l
1510 # Load color data from ROM
1512 # a1: color list address
1513 # d0: number of colors to load
1519 move.l d0,(GFXCNTL).l
1534 # d2 - tile_bits[15:11]
1559 # d3 - tile_bits[15:11]|digit_cnt[7:0]
1560 # destroys a0, preserves d3
1571 ror.l d0,d2 /* prep value */
1572 subq.l #1,d1 /* count */
1574 and.w #0xf800,d0 /* keep upper bits in d0 */
1587 dbra d1,_print_hex_loop
1592 # wait vertical sync interrupt
1603 # wait vsync start (polling)
1627 jmp 0x123456 /* will be patched */
1630 #################################################
1634 #################################################
1642 # vim:filetype=asmM68k