xiahanlu

设备内部对象定义

Oct 13th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.10 KB | None | 0 0
  1. /* private object for gameboy
  2.  *
  3.  * Copyright (C) 2018 moecmks
  4.  * This file is part of KS3578.
  5.  *
  6.  * do What The Fuck you want to Public License
  7.  *
  8.  * Version 1.0, March 2000
  9.  * Copyright (C) 2000 Banlu Kemiyatorn (]d).
  10.  * 136 Nives 7 Jangwattana 14 Laksi Bangkok
  11.  * Everyone is permitted to copy and distribute verbatim copies
  12.  * of this license document, but changing it is not allowed.
  13.  *
  14.  * Ok, the purpose of this license is simple
  15.  * and you just
  16.  *
  17.  * DO WHAT THE FUCK YOU WANT TO.
  18.  */
  19.  
  20. #ifndef _INTERNAL_H
  21. #define _INTERNAL_H 1
  22.  
  23. #include "conf.h"
  24. #include "gameboy.h"
  25. #include "list.h"
  26.  
  27. struct gameboy;
  28. struct controller;
  29. struct divider;
  30. struct timer;
  31. struct cpu;
  32. struct ppu;
  33. struct cartidge;
  34. struct serial;
  35. struct apu;
  36. struct machine_setup;
  37. struct romheader;
  38. /*-\
  39. \-*/
  40. static const
  41. struct machine_setup {
  42.   ks_double cpu_freq;  
  43.   ks_double vsync_freq;
  44.   ks_double clk_ms;
  45.   ks_double clk_ns;
  46.   ks_double frame_cycles;
  47.   ks_double line_cycles;
  48.   ks_double oam_cycles;
  49.   ks_double oambg_b_cycles;
  50.   ks_double oambg_hbl_cycles;
  51.   ks_double vbl_clk_last;
  52.   ks_double oambg_clk_st;
  53.   ks_double hbl_clk_st_b;
  54.   ks_double vbl_clk_st;
  55.   ks_double oam_clk_pick_per;
  56.   ks_double oam_clk_add_hbl_per;
  57.   ks_double cgb_gbp_p;
  58.   ks_double gbp_cgb_p;
  59.   ks_double oamdma_clks;
  60.   ks_double gdma_clks_b;
  61.   ks_double gdma_clks_per16; // always 7.63 us (DMG/CGB)
  62.   ks_double hdma_clks_per16; // always 8 us (DMG/CGB)
  63.   ks_double joypad_gen_freq;
  64. } std_machine = {
  65.   4194304.0, /* cpu_freq*/  
  66.   59.73, /* vsync_freq*/
  67.   4194304.0/1000.0, /* clk_ms*/
  68.   4194304.0/1000000.0, /* clk_ns*/
  69.   4194304.0/59.73, /* frame_cycles*/
  70.   4194304.0/59.73/154.0, /* line_cycles*/
  71.   80.992010239999985, /* oam_cycles*/
  72.   173.51835647999999, /* oambg_b_cycles*/
  73.   (70221.061443161/154.0) - 80.992010239999985, /* oambg_hbl_cycles*/
  74.   4559.809184621, /* vbl_clk_last*/
  75.   80.99201023999998, /* oambg_clk_st*/
  76.   173.51835647999999 + 80.992010239999985, /* hbl_clk_st_b*/
  77.   70221.061443161 - 4559.809184621, /* vbl_clk_st*/
  78.   80.992010239999985 / 10.0, /* oam_clk_pick_per*/
  79.   12.549357568000001, /* oam_clk_add_hbl_per*/
  80.   8400000.0/ 4194304.0, /* cgb_gbp_p*/
  81.   4194304.0/ 8400000.0, /* gbp_cgb_p*/
  82.   671.08864000000005, /* oamdma_clks*/
  83.   (4194304.0*220.0)/1000000.0, /* gdma_clks_b*/
  84.   (4194304.0*7.63)/1000000.0,/* gdma_clks_per16*/
  85.   (4194304.0*8.00)/1000000.0,/* hdma_clks_per16*/
  86.   (4194304.0)/JOYPAD_FREQ_IN_DMG /* joypad_gen_freq*/
  87. }, adv_machine = { 
  88.   8400000.0, /* cpu_freq*/  
  89.   59.73, /* vsync_freq*/
  90.   8400000.0/1000.0, /* clk_ms*/
  91.   8400000.0/1000000.0, /* clk_ns*/
  92.   8400000.0/59.73, /* frame_cycles*/
  93.   8400000.0/59.73/154.0, /* line_cycles*/
  94.   162.20400000000001, /* oam_cycles*/
  95.   347.50799999999998, /* oambg_b_cycles*/
  96.   (8400000.0/59.73/154.0) - 162.20400000000001, /* oambg_hbl_cycles*/
  97.   9132.0031048810633, /* vbl_clk_last*/
  98.   162.20400000000001, /* oambg_clk_st*/
  99.   347.50799999999998 + 162.20400000000001, /* hbl_clk_st_b*/
  100.   (8400000.0/59.73) - 9132.0031048810633, /* vbl_clk_st*/
  101.   162.20400000000001 / 10.0, /* oam_clk_pick_per*/
  102.   25.132800000000003, /* oam_clk_add_hbl_per*/
  103.   8400000.0/ 4194304.0, /* cgb_gbp_p*/
  104.   4194304.0/ 8400000.0, /* gbp_cgb_p*/
  105.   671.08864000000005, /* oamdma_clks*/
  106.   (8400000.0*110.0)/1000000.0, /* gdma_clks_b*/
  107.   (8400000.0*7.63)/1000000.0,/* gdma_clks_per16*/
  108.   (8400000.0*8.00)/1000000.0,/* hdma_clks_per16*/
  109.   (8400000.0)/JOYPAD_FREQ_IN_CGB /* joypad_gen_freq*/
  110. };
  111.  
  112. struct romheader {
  113.   ks_uint8 title[16];
  114.   ks_uint16 curlic;
  115.   ks_uint8 sgb;
  116.   ks_uint8 ctype;
  117.   ks_uint8 promsize;
  118.   ks_uint8 ramsize;
  119.   ks_uint8 targetcode;
  120.   ks_uint8 anclic;
  121.   ks_uint8 maskver;
  122.   ks_uint8 hdcrc;
  123.   ks_uint16 gcrc;
  124. };
  125. struct controller  {
  126.   ks_uint8 reg00_P1;
  127.  
  128.   /* for gamebot update host keybuf and IRQ */
  129.   void (*clks) (struct controller *);
  130.  
  131.   void *obj;
  132.   void *ubdata_user;
  133.   /* pad infos for gameboy*.*/
  134.   struct controller_pad gb_pad;
  135.   struct gameboy *gb;
  136. };
  137.  
  138. struct cpu {
  139.   /*  XXX:memory order dep. don't set struct align!*/
  140.   union { struct { ks_uint8 F; ks_uint8 A; }; ks_uint16 AF; };
  141.   union { struct { ks_uint8 C; ks_uint8 B; }; ks_uint16 BC; };
  142.   union { struct { ks_uint8 E; ks_uint8 D; }; ks_uint16 DE; };
  143.   union { struct { ks_uint8 L; ks_uint8 H; }; ks_uint16 HL; };
  144.   union { struct { ks_uint8 SL; ks_uint8 SH; }; ks_uint16 SP; };
  145.   union { struct { ks_uint8 PL; ks_uint8 PH; }; ks_uint16 PC; };
  146.  
  147.    /* Interrupt Master Enable*/
  148.   ks_uint8 IME;
  149.  
  150.    /* for Halt */
  151.   ks_bool halt;  
  152.  
  153.    /* for stop */
  154.   ks_bool stop;
  155.  
  156.    /* for halt bug */
  157.   ks_int _backup;
  158.  
  159.    /* for speed mode */
  160.   ks_uint8 reg4D_key1;
  161.  
  162.    /* gameboy object  */
  163.   struct gameboy *gb;              
  164. };
  165. struct oam {
  166.   union {
  167.     struct {
  168.       ks_uint8 y;
  169.       ks_uint8 x;
  170.       ks_uint8 id;
  171.       ks_uint8 attr;
  172.     };
  173.     ks_uint8 blk[4];
  174.   };
  175. };
  176. struct pal {
  177.   union {
  178.     struct {
  179.       ks_uint8 _lo;
  180.       ks_uint8 _hi;
  181.     };
  182.     ks_uint16 rgb15;
  183.     ks_uint8 blk[2];
  184.   };
  185. };
  186.  
  187. /* for ppu:: spi_ca */
  188. #define PIXEL_SPRITE_NOTRANS 1
  189. #define PIXEL_SPRITE_BACK 2
  190.  
  191. /* LCDC Status MASK */
  192. #define LCDS_MODE_FLAG_HBLANK 0
  193. #define LCDS_MODE_FLAG_VLANK 1
  194. #define LCDS_MODE_FLAG_SERACH_OAM 2
  195. #define LCDS_MODE_FLAG_SERACH_OAMVRAM 3
  196. #define LCDS_MODE_FLAG_ALL_MASK 3
  197.  
  198. /* LCDC Status Interrupt MASK */
  199. #define LCDS_INTERRUPET_LINE_MASK 0x40
  200. #define LCDS_INTERRUPET_HBLANK_MASK 0x10
  201. #define LCDS_INTERRUPET_VBLANK_MASK 0x08
  202. #define LCDS_INTERRUPET_OAM_MASK 0x20
  203. #define LCDS_INTERRUPET_ALL_MASK (LCDS_INTERRUPET_LINE_MASK|LCDS_INTERRUPET_HBLANK_MASK|LCDS_INTERRUPET_VBLANK_MASK|LCDS_INTERRUPET_OAM_MASK)
  204.  
  205. /* LCDC Control MASK */
  206. #define LCDC_DISPLAY_MASK 0x80
  207. #define LCDC_WINDOW_MASK 0x20
  208. #define LCDC_OAM_SIZE16_MASK 0x04
  209. #define LCDC_OAM_MASK 0x02
  210.  
  211. #define LCD_OAM_FLIP_Y_MASK 0x40
  212. #define LCD_OAM_FLIP_X_MASK 0x20
  213. #define LCD_OAM_BACKGROUND_MASK 0x80
  214.  
  215. struct ppu {
  216.   ks_uint16 *bufb; /* for alignmem */
  217.   ks_uint8 ram[0x4000];  /* 8K for DMG, 16 for CGB */
  218.  
  219.   struct _oamlineframe /* sprite line buffer cache *.*/
  220.   {
  221.     ks_uint16 attr;
  222.     ks_uint16 pixel;
  223.   } olf[176];
  224.  
  225.   struct gameboy *gb;
  226.   struct oam sp[40];
  227.   struct pal bg_pal[8][4]; /* pal for CGB */
  228.   struct pal sp_pal[8][4]; /* pal for CGB */
  229.   struct ppu_framebuffer fmebuf;
  230.  
  231.   ks_uint16 bg_pal_dmg[4]; /* pal for DMG */
  232.   ks_uint16 sp_pal_dmg[2][4]; /* pal for DMG */
  233.  
  234.   ks_uint16 bg_pal_dmgT[4]; /* pal for DMG */
  235.   ks_uint16 sp_pal_dmgT[2][4]; /* pal for DMG */
  236.  
  237.   ks_uint8 reg40_LCDC;
  238.   ks_uint8 reg40_NMIf;
  239.   ks_uint8 reg41_LCDS;  
  240.   ks_uint8 reg41_LCDM_T;
  241.   ks_uint8 reg41_IRQf;
  242.   ks_uint8 reg42_SCY;
  243.   ks_uint8 reg43_SCX;    
  244.   ks_uint8 reg44_LY;
  245.   ks_uint8 reg44_LY_T;
  246.   ks_uint8 reg45_LYC;
  247.   ks_uint8 reg46_DMA;
  248.   ks_uint8 reg47_BGP;
  249.   ks_uint8 reg48_OBP0;
  250.   ks_uint8 reg49_OBP1;
  251.   ks_uint8 reg4A_WY;
  252.   ks_uint8 reg4A_WYRSC;
  253.   ks_uint8 reg4A_WYLineHit;
  254.   ks_uint8 reg4B_WX;
  255.   ks_uint8 reg4F_VBK;
  256.   ks_uint8 reg51_HDMA1;
  257.   ks_uint8 reg52_HDMA2;
  258.   ks_uint8 reg53_HDMA3;
  259.   ks_uint8 reg54_HDMA4;
  260.   ks_uint8 reg55_HDMA5;
  261.   ks_uint8 reg68_BCPS;
  262.   ks_uint8 reg69_BCPD;
  263.   ks_uint8 reg6A_OCPS;
  264.   ks_uint8 reg6B_OCPD;
  265.  
  266.   ks_int32 vscan;
  267.   ks_int32 vscanR;
  268.   ks_int32 vscan40;
  269.   ks_int32 xscanR;
  270.   ks_int32 uscan;
  271.   ks_int32 uscanR;
  272.  
  273.   ks_bool hdma_gen;
  274.  
  275.   ks_uint16 hdma_src;
  276.   ks_uint16 hdma_dst;
  277.   ks_uint16 hdma_r16;
  278.  
  279.   ks_double hdma_clk;
  280.   ks_double hbl_clks_st;
  281.   ks_double oambg_clks_divider21;
  282.  
  283.   void (*bgwin_done) (struct ppu *_5028, ks_int16 scanline);
  284.   void (*sprite_done) (struct ppu *_5028, ks_int delta);
  285.   void (*device_blit) (struct ppu *_5028, void *obj, struct ppu_framebuffer *fbuf); /*blit for host devcice */
  286.  
  287.   void *obj;
  288.   void (*clks) (struct ppu *);
  289. };
  290. struct gameboy {
  291.   struct controller *joypad;
  292.   struct cpu *lr35902;
  293.   struct ppu *lh5028;
  294.   struct apu *apu;
  295.   struct cartridge *cart;
  296.   struct divider *divider;
  297.   struct timer *timer;
  298.   struct serial *serial;
  299.   struct machine_setup *mach_tools;
  300.  
  301.   ks_double cpu_clks;
  302.   ks_double cpu_clks_total;
  303.   ks_double cpu_clks_timer; // cur full - block
  304.   ks_double cpu_clks_timerdbg; // cur full - block
  305.   ks_double cpu_clks_joypad; // cur full - block
  306.   ks_double cpu_clks_divider; // cur full - block
  307.   ks_double cpu_clks_ppu; // cur full - block
  308.   ks_double cpu_clks_apu; // cur full - block
  309.   ks_double cpu_clks_cart; // cur full - block
  310.   ks_double cpu_clks_serial; // RS232 Serial Port for CGB/ DMG.
  311.   ks_double cpu_clks_dma;
  312.   ks_double deflect_ms;
  313.  
  314.   ks_uint8 reg01_SB;
  315.   ks_uint8 reg02_SC;
  316.  
  317.   ks_uint8 reg0F_IF; /* interrupt flags register */
  318.   ks_uint8 reg56_IC;
  319.   ks_uint8 reg70_SVBK;
  320.   ks_uint8 regFF_IE; /* Interrupt enable register */
  321.  
  322.   /* work ram. */
  323.   ks_uint8 wram[0x8000];
  324.   /* high ram. */
  325.   ks_uint8 hram[0x200];
  326.   /* unknow mem, io read/write */
  327.   ks_uint8 unknow_ram[0x10000];
  328.  
  329.   /* controller drv */
  330.   void (*controller_hostdrv)
  331.              (struct gameboy *, void *controller_drvobj,
  332.                 struct controller_pad *, /* gb-self for recv joypadbuffer */
  333.              struct controller_pad * /* host-edge 1?pulse gen:nodone */);
  334.   void *controller_drvobj;
  335.  
  336.   /* display drv */
  337.   void (*display_hostdrv)
  338.              (struct gameboy *, void *display_drvobj,
  339.                 struct ppu_framebuffer *fmebuf);
  340.   void *display_drvobj;
  341.   /* sound drv */
  342.  
  343.  
  344. };
  345. struct timer {
  346.   ks_uint8 reg05_TIMA;
  347.   ks_uint8 reg06_TMA;
  348.   ks_uint8 reg07_TAC;
  349.   ks_double freq_tab[4];
  350.  
  351.   void (*clks) (struct timer *);
  352.   ks_double timestamp;
  353.  
  354.   struct gameboy *gb;
  355. };
  356. struct divider {
  357.   ks_uint8 reg04_DIV;
  358.   ks_double freq;
  359.  
  360.   void (*clks) (struct divider *);
  361.   ks_double timestamp;
  362.   struct gameboy *gb;
  363. };
  364.  
  365. struct apu {
  366.  
  367.   ks_uint8 reg10_NR10;
  368.   ks_uint8 reg11_NR11;
  369.   ks_uint8 reg12_NR12;
  370.   ks_uint8 reg13_NR13;
  371.   ks_uint8 reg14_NR14;
  372.   ks_uint8 reg16_NR21;
  373.   ks_uint8 reg17_NR22;
  374.   ks_uint8 reg18_NR23;
  375.   ks_uint8 reg19_NR24;
  376.   ks_uint8 reg1A_NR30;
  377.   ks_uint8 reg1B_NR31;
  378.   ks_uint8 reg1C_NR32;
  379.   ks_uint8 reg1D_NR33;
  380.   ks_uint8 reg1E_NR34;
  381.   ks_uint8 reg20_NR41;
  382.   ks_uint8 reg21_NR42;
  383.   ks_uint8 reg22_NR43;
  384.   ks_uint8 reg23_NR44;
  385.   ks_uint8 reg24_NR50;
  386.   ks_uint8 reg25_NR51;
  387.   ks_uint8 reg26_NR52;
  388.   ks_uint8 reg30_AUD3WAVERAM;
  389.   ks_uint8 reg31_AUD3WAVERAM;
  390.   ks_uint8 reg32_AUD3WAVERAM;
  391.   ks_uint8 reg33_AUD3WAVERAM;
  392.   ks_uint8 reg34_AUD3WAVERAM;
  393.   ks_uint8 reg35_AUD3WAVERAM;
  394.   ks_uint8 reg36_AUD3WAVERAM;
  395.   ks_uint8 reg37_AUD3WAVERAM;
  396.   ks_uint8 reg38_AUD3WAVERAM;
  397.   ks_uint8 reg39_AUD3WAVERAM;
  398.   ks_uint8 reg3A_AUD3WAVERAM;
  399.   ks_uint8 reg3B_AUD3WAVERAM;
  400.   ks_uint8 reg3C_AUD3WAVERAM;
  401.   ks_uint8 reg3D_AUD3WAVERAM;
  402.   ks_uint8 reg3E_AUD3WAVERAM;
  403.   ks_uint8 reg3F_AUD3WAVERAM;
  404.   ks_uint8 reg76_PCMChannel1_2;
  405.   ks_uint8 reg77_PCMChannel3_4;
  406.  
  407.   void (*clks) (struct apu *);
  408.   struct gameboy *gb;
  409. };
  410. struct serial {
  411.   void (*clks) (struct serial *);
  412.   struct gameboy *gb;
  413. };
  414.  
  415. /* cartridge device type */
  416. #define MBC_0 0
  417. #define MBC_1 1
  418. #define MBC_2 2
  419. #define MBC_3 3
  420. #define MBC_4 4
  421. #define MBC_5 5
  422. #define MBC_6 6
  423. #define MBC_7 7
  424. #define TAMA5 8
  425. #define HUCL1 9
  426. #define HUCL3 10
  427. #define MMM0 11
  428. #define POCKER_CAM 12
  429.  
  430. /* MBC1 */
  431. #define MBC1_MODE0_2MROM_8KRAM 0
  432. #define MBC1_MODE1_512KROM_32KRAM 1
  433. typedef int mbc1_cart_mode;
  434.  
  435. struct mbc1_chip {
  436.   mbc1_cart_mode mode;
  437.   ks_bool ram_en;
  438.   ks_uint16 prombank;
  439.   ks_uint16 srambank;
  440.   ks_uint16 bankcac;
  441. };
  442. struct cartridge {
  443.   struct romheader header;
  444.   ks_bool s_latch;
  445.   ks_bool battery;
  446.   ks_uint8 *promworks;
  447.   ks_uint8 *sramworks;
  448.   ks_uint8 promsize;
  449.   ks_uint8 sramsize;
  450.  
  451.   void (*clks) (struct cartridge *);
  452.   ks_uint8 (*read) (struct cartridge *cartridge, ks_uint16 address);
  453.   ks_void (*write) (struct cartridge *cartridge, ks_uint16 address, ks_uint8 value);
  454.  
  455.   union {
  456.     struct mbc1_chip *mbc1;
  457.     void *ubdata_user;
  458.   };
  459.   struct gameboy *gb;    
  460. };
  461.  
  462. #define IRQ_1 0x01  /*  VBLANK (NMI)  */
  463. #define IRQ_2 0x02  /*  LCDC  */
  464. #define IRQ_3 0x04  /*  Programmable timer */
  465. #define IRQ_4 0x08  /*  Serial port switching */
  466. #define IRQ_5 0x10  /*  P14-15 Descent edge acknowledge */
  467. #define IRQ_NIL 0xFF  /*  P14-15 Descent edge acknowledge */
  468.  
  469. #define IRQ_1_ADDRESS 0x40 /*  VBLANK (NMI)  */
  470. #define IRQ_2_ADDRESS 0x48 /*  LCDC  */
  471. #define IRQ_3_ADDRESS 0x50 /*  Programmable timer */
  472. #define IRQ_4_ADDRESS 0x58 /*  Serial port switching */
  473. #define IRQ_5_ADDRESS 0x60 /*  P14-15 Descent edge acknowledge */
  474.  
  475. /* gameboy internal done **/
  476. int gameboy_resume_ifstop (struct gameboy *gb);
  477. double gameboy_getcycles (struct gameboy *gb);
  478. double gameboy_get_ppuclks (struct gameboy *gb, ks_bool *stride_frame);
  479. void gameboy_setcycles (struct gameboy *gb, double cycles);
  480. ks_uint8 ks_callstd gameboy_mmu_read (struct gameboy *gb, ks_uint16 address);
  481. ks_uint16 ks_callstd gameboy_mmu_read_w (struct gameboy *gb, ks_uint16 address);
  482. ks_void ks_callstd gameboy_mmu_write (struct gameboy *gb, ks_uint16 address, ks_uint8 value);
  483. ks_void ks_callstd gameboy_mmu_write_w (struct gameboy *gb, ks_uint16 address, ks_uint16 value);
  484. int gameboy_resume_ifstop (struct gameboy *gb);
  485. int  gameboy_hdma_copy (struct gameboy *gb);
  486.  
  487. #endif
Add Comment
Please, Sign In to add comment