Advertisement
crabb

kappabox 2020-24-06

Jun 24th, 2020
1,988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.55 KB | None | 0 0
  1. /****************************************************
  2.  *           KAPPA BOX for SEGA SATURN            
  3.  *             BY SUWACRAB 2020-21-06
  4.  *            REVISION DATE 2020-24-06
  5. *****************************************************/
  6. #ifndef KBOX_H
  7. #define KBOX_H
  8.  
  9. /*----- type defines ------------------------------------------------*/
  10. typedef unsigned char u8;
  11. typedef unsigned short u16;
  12. typedef unsigned int u32;
  13.  
  14. typedef signed char s8;
  15. typedef signed short s16;
  16. typedef signed int s32;
  17.  
  18. #define INLINE static inline
  19.  
  20. /*----- RAM defines -------------------------------------------------*/
  21. #define PACKED   __attribute__((packed))
  22. #define ALIGN(n) __attribute__((aligned(n)))
  23.  
  24. #define WRAM_LO ((volatile u8*)(0x20200000))
  25. #define WRAM_HI ((volatile u8*)(0x26000000))
  26. #define SCU_MEM ((volatile u8*)(0x25FE0000))
  27. #define DMA_MEM ((volatile u8*)(0xFFFFFF80))
  28.  
  29. /*----- DMA defines -------------------------------------------------*/
  30. typedef struct CH_DMA {
  31.   u32 src;                         /* $00 (SAR, source address)      */
  32.   u32 dst;                         /* $04 (DAR, destination address) */
  33.   u32 cnt;                         /* $08 (TCR, transfer count)      */
  34.   u32 ctrl;                        /* $0C (CHCR, control, i guess)   */
  35. } ALIGN(2) CH_DMA;
  36.  
  37. typedef enum dma_sizes {
  38.   DMA_SIZE_BYTE,                                  /* $01 byte.       */
  39.   DMA_SIZE_WORD,                                  /* $02 bytes.      */
  40.   DMA_SIZE_LONG,                                  /* $04 bytes.      */
  41.   DMA_SIZE_128B                                   /* $10 bytes!      */
  42. } dma_sizes;
  43.  
  44. // DMA channels, operation register, control registers
  45. #define DMA_REG ((volatile CH_DMA*)DMA_MEM)
  46. #define DMA_REG_OR ((volatile u32*)(0xFFFFFFB0))
  47. #define DMA_REG_CR ((volatile u8*)(0xFFFFFE71))
  48. // DMA destination is fixed, dest is incremented, dest is decremented
  49. // > used for changing the direction that the destination
  50. // > address goes during a transfer.
  51. #define DMA_DST_FIX (0<<14)
  52. #define DMA_DST_INC (1<<14)
  53. #define DMA_DST_DEC (2<<14)
  54. // DMA source fixed, src increments, src decrements
  55. // > same as the above, except with the source address.
  56. #define DMA_SRC_FIX (0<<12)
  57. #define DMA_SRC_INC (1<<12)
  58. #define DMA_SRC_DEC (2<<12)
  59. // DMA size
  60. // > controls the size of each unit transferred.
  61. // > $0 is 1 byte, $1 is 2 bytes, $2 is 4 bytes, $3 is 16 bytes.
  62. #define DMA_SIZE(n) (((n)&3)<<10)
  63.  
  64. // DMA auto-request, interrupt enable, transfer-end, enable
  65. #define DMA_AR (1<<9)
  66. #define DMA_IE (1<<2)
  67. #define DMA_TE (1<<1)
  68. #define DMA_ON (1)
  69.  
  70. // DMA master enable,NMIF,address error, priority
  71. #define DMA_DME (1)
  72. #define DMA_NMIF (1<<1)
  73. #define DMA_AE (1<<2)
  74. #define DMA_PR (1<<3)
  75.  
  76. // stop all DMA transfers
  77. INLINE void sh2_dma_stop()
  78. { *DMA_REG_OR &= (DMA_AE | DMA_PR | DMA_NMIF); }
  79. // initialize DMA channels
  80. INLINE void sh2_dma_init()
  81. {
  82.   sh2_dma_stop();
  83.   DMA_REG[0].ctrl = 0;
  84.   DMA_REG[1].ctrl = 0;
  85. }
  86. // check if DMA channel `ch` is active
  87. INLINE u32 sh2_dma_active(u32 ch)
  88. { return (!(DMA_REG[ch].ctrl&DMA_TE)) ? 1 : 0; }
  89. // blasts from `src` to `dst`, in `cnt` amount of transfers, using `mode`.
  90. INLINE void sh2_dma_blast(u32 ch, void *src,void *dst,u32 cnt,u32 mode)
  91. {
  92.   // "why are you using assembly?" it's easier to debug in an emu, trust me.
  93.   // alternatively, you may comment this part out and just use
  94.   // the C section (ha) below.
  95.   asm(
  96.     // move src,dst,cnt
  97.     "MOV.L %[src],@(0,%[ch])\t\n"
  98.     "MOV.L %[dst],@(4,%[ch])\t\n"
  99.     "MOV.L %[cnt],@(8,%[ch])\t\n"
  100.     // REG_CR[ch] = 0
  101.     "MOV #0,R0\t\n"
  102.     "MOV.B R0,@%[cr]\t\n"
  103.     // move ctrl to DMA_REG[ch].ctrl
  104.     "MOV.L @(12,%[ch]),R0\t\n" // read from ctrl before writing.
  105.     "MOV.L %[mode],@(12,%[ch])\t\n"
  106.     :: [ch]"r"(((u32)&DMA_REG[ch])),[src]"r"(src),
  107.     [dst]"r"(dst),[cnt]"r"(cnt),
  108.     [mode]"r"(mode),[cr]"r"(((u32)&DMA_REG_CR[ch]))
  109.   ); // */
  110.  
  111.   /* DMA_REG[ch].src = (u32)src;
  112.   DMA_REG[ch].dst = (u32)dst;
  113.   DMA_REG[ch].cnt = cnt;
  114.   DMA_REG_CR[ch] = 0; // external request
  115.   DMA_REG[ch].ctrl = (DMA_REG[ch].ctrl&0) | mode; // */
  116. }
  117.  
  118. /* DMA example: copy $10 bytes from WRAM_LO[$000000] to WRAM_LO[$000100]
  119.   * sh2_dma_init();
  120.   * DMA_REG_OR = DMA_DME | DMA_PR;
  121.   *
  122.   * sh2_dma_blast(0,
  123.   *   &WRAM_LO[0],&WRAM_LO[0x100],0x10,
  124.   *   DMA_SRC_INC|DMA_DST_INC|DMA_AR|DMA_ON
  125.   * );
  126.   * // DMA_SRC_INC is so the source address increases
  127.   * // DMA_DST_INC is so the destination address increases
  128.   * // DMA_AR is so it the transfer occurs immediately...?
  129.   ** idk what its actually for but if u dont put it in it doesn't copy
  130. */
  131.  
  132. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement