don't click here

MD is Weird.

Discussion in 'Engineering & Reverse Engineering' started by Weird Person, Nov 5, 2006.

Thread Status:
Not open for further replies.
  1. Weird Person

    Weird Person

    You lost two seconds reading this Member
    367
    0
    0
    Who knows?
    I'm developing a easy-to-use subroutine that allows you load any sort of data to CRAM,VRAM or VSRAM, and I got a subroutine that looks perfect! Look at the code below:

    Code (Text):
    1. Main:
    2.    
    3.     move.b #$03,d0
    4.     move.w #$0020,d1
    5.     move.l #Pallete,d2
    6.     move.w #EndPallete-Pallete,d3
    7.     jsr Proc_LoadGraphics
    8.  
    9.     move.b #$01,d0
    10.     move.w #$0020,d1
    11.     move.l #Vram,d2
    12.     move.w #EndVram-Vram,d3
    13.     jsr Proc_LoadGraphics
    14.    
    15. EternalLoop:
    16.     bra.s EternalLoop
    17.     rts
    18.        
    19. Pallete:
    20.     dc.w $0EEE,$068E,$0E86,$0E0E,$066E,$0E66
    21. EndPallete:
    22.  
    23. Vram:
    24.     dc.w $0E22,$068E,$0E86,$0E0E,$066E,$0E66
    25.     dc.w $0E22,$0444,$0444,$0E0E,$066E,$0E66
    26.     dc.w $0E10,$0444,$0E86,$0444,$066E,$0E66   
    27.     dc.w $0EEE,$068E,$0E86,$0E0E,$0444,$0E66   
    28.     dc.w $0EEE,$068E,$0E86,$0E0E,$066E,$0E66   
    29.     dc.w $0EEE,$068E,$0E86,$0E0E,$066E,$0E66   
    30. EndVram:
    31. ;-------------------------------;  
    32. ; Procedures
    33. ;-------------------------------;
    34.  
    35. ;-------------------------------;
    36. ; Proc_LoadGraphics
    37. ;-------------------------------;
    38.  
    39. Proc_LoadGraphics:
    40.  
    41. ; d0.b = Code
    42. ; d1.w = Adress (in CRAM, VRAM or VSRAM...)
    43. ; d2.l = Source Data Adress
    44. ; d3.w = How many bytes to move.
    45. ; d4.l = Temporary Register.
    46.  
    47.     and.l #$0000FFFF,d1 ; Clear shitty data.
    48.     rol.l #2,d1
    49.     ror.w #2,d1
    50.     swap.w d1
    51.  
    52. Proc_LoadGraphics_Switch1:
    53.  
    54.     cmp.b #1,d0
    55.     beq Proc_LoadGraphics_Switch1_1
    56.  
    57.     cmp.b #2,d0
    58.     beq Proc_LoadGraphics_Switch1_2
    59.  
    60.     cmp.b #3,d0
    61.     beq Proc_LoadGraphics_Switch1_3
    62.  
    63.     bra Proc_LoadGraphics_Switch1_Default
    64.  
    65. Proc_LoadGraphics_Switch1_1:
    66. ; You choosed: VRAM
    67.  
    68.     move.l #$40000000,d0
    69.     bra.s Proc_LoadGraphics_EndSwitch1
    70.  
    71. Proc_LoadGraphics_Switch1_2:
    72. ; You choosed: VSRAM
    73.  
    74.     move.l #$40000010,d0
    75.     bra.s Proc_LoadGraphics_EndSwitch1
    76.  
    77. Proc_LoadGraphics_Switch1_3:
    78. ; You choosed: CRAM
    79.  
    80.     move.l #$C0000000,d0
    81.     bra.s Proc_LoadGraphics_EndSwitch1
    82.  
    83. Proc_LoadGraphics_Switch1_Default:
    84. ; Invalid Choice.
    85.  
    86.     moveq #0,d0
    87.     bra EndProc_LoadGraphics
    88.  
    89. Proc_LoadGraphics_EndSwitch1:
    90.  
    91.     add.l d1,d0    
    92.     move.l d0,($C00004).l
    93.     move.l a0,d4
    94.     move.l d2,a0
    95.     lsr.w #1,d3 ; d3 = d3 / 2
    96.     sub.w #1,d3 ; d3 = d3 - 1
    97.  
    98. Proc_LoadGraphics_Loop1:
    99.  
    100.     move.w  (a0)+,($C00000).l
    101.     dbra    d3,Proc_LoadGraphics_Loop1
    102.  
    103. Proc_LoadGraphics_EndLoop1:
    104.  
    105.     move.l d4,a0
    106.  
    107. EndProc_LoadGraphics:
    108.     rts
    The code above worked pretty well for me. (It's a piece of code. You can get the full code here)

    Now, Think there is a file named "vram.bin" with a 62,5kb size (fits in VRAM.)
    Look at what's between the Labels "Vram" and "EndVram". It's some random data that is loaded at the VRAM. Change it to " incbin "vram.bin" ". And the game will crash!

    The procedure "LoadGraphics" works pretty well in load small pieces of data (1Kb, 2Kb, 3Kb, 4Kb...), but It fails why loading big pieces of data (62Kb...). I looked over and over the code, and I see no reason for that happen. Both datas (1Kb and 62,5Kb) fits in VRAM. Can someone help me?
     
  2. drx

    drx

    mfw Researcher
    2,254
    350
    63
    :rolleyes:
    It's probably because you're loading data to the parts of VRAM which store things like sprite data, plane data. While planes should be fine, loading bad stuff to the sprites data can crash it (putting stupid stuff to the link field? setting wrong coords? too much stuff on one line/screen... the possibilities are endless). And there's also scrolling etc.

    Also, if you have something after your EndVram label (some data, code) and you don't set it so the stuff after the label will get an even address, it'll also crash.
     
  3. Weird Person

    Weird Person

    You lost two seconds reading this Member
    367
    0
    0
    Who knows?
    Thank you Drx! It was the sprites. I was loading $FA00 bytes. The Sprite Adress in VRAM is $F800. Just cut $200 bytes and it worked perfectly.

    [EDIT] Binary rom image? Ok.
    Nothing too much amazing, but now, MD programming is much more easier for me.
    Or do you prefer use DMA 0_o? (Too hard to make a procedure.)
     
  4. drx

    drx

    mfw Researcher
    2,254
    350
    63
    :rolleyes:
    Code (Text):
    1. ; -----------------------------------------------------
    2. ;  This subroutine sets the DMA (Direct Memory Access) to 68k to VRAM
    3. ;
    4. ;   d1.l - DMA source address (in 68k memory)
    5. ;   d2.w - DMA destination address (in VRAM)
    6. ;   d3.w - DMA length
    7. ; -----------------------------------------------------
    8.  
    9.  SetupDMA_68kToVRam:     
    10.  
    11.                  move.w  #$9300,d0
    12.                  move.b  d3,d0
    13.                  move.w  d0,($c00004).l    ; setup VDP register $13 - DMA length bits 7-0
    14.  
    15.                  move.w  #$9400,d0
    16.                  lsr.w   #8,d3
    17.                  move.b  d3,d0
    18.                  move.w  d0,($c00004).l    ; setup VDP register $14 - DMA length bits 15-8
    19.  
    20.                  move.w  #$9500,d0
    21.                  lsr.l   #1,d1
    22.                  move.b  d1,d0
    23.                  move.w  d0,($c00004).l    ; setup VDP register $15 - 68k addr bits 8-1 (remember, the bit 0 is always %0!)
    24.  
    25.                  move.w  #$9600,d0
    26.                  lsr.l   #8,d1
    27.                  move.b  d1,d0
    28.                  move.w  d0,($c00004).l    ; setup VDP register $16 - 68k addr bits 16-9
    29.  
    30.                  move.w  #$9700,d0
    31.                  lsr.l   #8,d1
    32.                  andi.b  #$7F,d1; ''
    33.                  move.b  d1,d0
    34.                  move.w  d0,($c00004).l    ; setup VDP register $17 - 68k addr bits 23-17
    35.  
    36.                  andi.l  #$FFFF,d2    ; VRAM destination address
    37.                  lsl.l   #2,d2
    38.                  lsr.w   #2,d2
    39.                  swap   d2           ; VRAM destination address, edited to suit VDP =P
    40.                  ori.l   #$40000080,d2  ; set up the 68k to VRAM DMA, start it
    41.                  move.l  d2,($c00004).l
    42.  
    43.                  move.w  #0,($c00000).l
    44.  
    45.                  rts
    You might want to set register $0f to 01 before the DMA and back to 02 after DMA to make it work.
     
Thread Status:
Not open for further replies.