1 // license:BSD-3-Clause
2 // copyright-holders:Wilbert Pol
3 /*****************************************************************************
4  *
5  * includes/gb.h
6  *
7  ****************************************************************************/
8 #ifndef MAME_INCLUDES_GB_H
9 #define MAME_INCLUDES_GB_H
10 
11 #pragma once
12 
13 #include "sound/gb.h"
14 #include "cpu/lr35902/lr35902.h"
15 #include "bus/gameboy/gb_slot.h"
16 #include "machine/ram.h"
17 #include "video/gb_lcd.h"
18 #include "emupal.h"
19 
20 
21 class gb_state : public driver_device
22 {
23 public:
gb_state(const machine_config & mconfig,device_type type,const char * tag)24 	gb_state(const machine_config &mconfig, device_type type, const char *tag) :
25 		driver_device(mconfig, type, tag),
26 		m_cartslot(*this, "gbslot"),
27 		m_maincpu(*this, "maincpu"),
28 		m_apu(*this, "apu"),
29 		m_region_maincpu(*this, "maincpu"),
30 		m_rambank(*this, "cgb_ram"),
31 		m_inputs(*this, "INPUTS"),
32 		m_bios_hack(*this, "SKIP_CHECK"),
33 		m_ram(*this, RAM_TAG),
34 		m_ppu(*this, "ppu"),
35 		m_palette(*this, "palette")
36 	{ }
37 
38 	uint8_t       m_gb_io[0x10];
39 
40 	/* Timer related */
41 	uint16_t      m_divcount;
42 	uint8_t       m_shift;
43 	uint16_t      m_shift_cycles;
44 	uint8_t       m_triggering_irq;
45 	uint8_t       m_reloading;
46 
47 	/* Serial I/O related */
48 	uint16_t      m_internal_serial_clock;
49 	uint16_t      m_internal_serial_frequency;
50 	uint32_t      m_sio_count;             /* Serial I/O counter */
51 
52 	/* SGB variables */
53 	int8_t m_sgb_packets;
54 	uint8_t m_sgb_bitcount;
55 	uint8_t m_sgb_bytecount;
56 	uint8_t m_sgb_start;
57 	uint8_t m_sgb_rest;
58 	uint8_t m_sgb_controller_no;
59 	uint8_t m_sgb_controller_mode;
60 	uint8_t m_sgb_data[0x100];
61 
62 	/* CGB variables */
63 	uint8_t       *m_gbc_rammap[8];           /* (CGB) Addresses of internal RAM banks */
64 	uint8_t       m_gbc_rambank;          /* (CGB) Current CGB RAM bank */
65 
66 	bool m_bios_disable;
67 
68 	void gb_io_w(offs_t offset, uint8_t data);
69 	void gb_io2_w(offs_t offset, uint8_t data);
70 	void sgb_io_w(offs_t offset, uint8_t data);
71 	uint8_t gb_ie_r();
72 	void gb_ie_w(uint8_t data);
73 	uint8_t gb_io_r(offs_t offset);
74 	void gbc_io_w(offs_t offset, uint8_t data);
75 	void gbc_io2_w(offs_t offset, uint8_t data);
76 	uint8_t gbc_io2_r(offs_t offset);
77 	void gb_palette(palette_device &palette) const;
78 	DECLARE_MACHINE_START(sgb);
79 	DECLARE_MACHINE_RESET(sgb);
80 	void sgb_palette(palette_device &palette) const;
81 	void gbp_palette(palette_device &palette) const;
82 	DECLARE_MACHINE_START(gbc);
83 	DECLARE_MACHINE_RESET(gbc);
84 	void gbc_palette(palette_device &palette) const;
85 	void gb_timer_callback(uint8_t data);
86 
87 	uint8_t gb_cart_r(offs_t offset);
88 	uint8_t gbc_cart_r(offs_t offset);
89 	void gb_bank_w(offs_t offset, uint8_t data);
90 	uint8_t gb_ram_r(offs_t offset);
91 	void gb_ram_w(offs_t offset, uint8_t data);
92 	uint8_t gb_echo_r(address_space &space, offs_t offset);
93 	void gb_echo_w(address_space &space, offs_t offset, uint8_t data);
94 	optional_device<gb_cart_slot_device> m_cartslot;
95 
96 	void supergb(machine_config &config);
97 	void supergb2(machine_config &config);
98 	void gbcolor(machine_config &config);
99 	void gbpocket(machine_config &config);
100 	void gameboy(machine_config &config);
101 	void gameboy_map(address_map &map);
102 	void gbc_map(address_map &map);
103 	void sgb_map(address_map &map);
104 
105 protected:
106 	enum {
107 		SIO_ENABLED = 0x80,
108 		SIO_FAST_CLOCK = 0x02,
109 		SIO_INTERNAL_CLOCK = 0x01
110 	};
111 
112 	required_device<lr35902_cpu_device> m_maincpu;
113 	required_device<gameboy_sound_device> m_apu;
114 	required_memory_region m_region_maincpu;
115 	optional_memory_bank m_rambank;   // cgb
116 	required_ioport m_inputs;
117 	required_ioport m_bios_hack;
118 	optional_device<ram_device> m_ram;
119 	required_device<dmg_ppu_device> m_ppu;
120 	required_device<palette_device> m_palette;
121 
122 	void gb_timer_increment();
123 	void gb_timer_check_irq();
124 	void gb_init();
125 	void gb_init_regs();
126 	void gb_serial_timer_tick();
127 
128 	void save_gb_base();
129 	void save_gbc_only();
130 	void save_sgb_only();
131 
132 	virtual void machine_start() override;
133 	virtual void machine_reset() override;
134 };
135 
136 
137 class megaduck_state : public gb_state
138 {
139 public:
megaduck_state(const machine_config & mconfig,device_type type,const char * tag)140 	megaduck_state(const machine_config &mconfig, device_type type, const char *tag) :
141 		gb_state(mconfig, type, tag),
142 		m_cartslot(*this, "duckslot")
143 	{ }
144 
145 	void megaduck(machine_config &config);
146 
147 protected:
148 	virtual void machine_start() override;
149 	virtual void machine_reset() override;
150 
151 private:
152 	uint8_t megaduck_video_r(offs_t offset);
153 	void megaduck_video_w(offs_t offset, uint8_t data);
154 	void megaduck_sound_w1(offs_t offset, uint8_t data);
155 	uint8_t megaduck_sound_r1(offs_t offset);
156 	void megaduck_sound_w2(offs_t offset, uint8_t data);
157 	uint8_t megaduck_sound_r2(offs_t offset);
158 	void megaduck_palette(palette_device &palette) const;
159 	void megaduck_map(address_map &map);
160 
161 	uint8_t cart_r(offs_t offset);
162 	void bank1_w(offs_t offset, uint8_t data);
163 	void bank2_w(offs_t offset, uint8_t data);
164 	required_device<megaduck_cart_slot_device> m_cartslot;
165 };
166 
167 #endif // MAME_INCLUDES_GB_H
168