1 // license:BSD-3-Clause
2 // copyright-holders:Ryan Holtz, David Haywood
3 
4 #include "emu.h"
5 #include "includes/spg2xx.h"
6 
7 #include "bus/generic/slot.h"
8 #include "bus/generic/carts.h"
9 
10 
11 class vii_state : public spg2xx_game_state
12 {
13 public:
vii_state(const machine_config & mconfig,device_type type,const char * tag)14 	vii_state(const machine_config &mconfig, device_type type, const char *tag) :
15 		spg2xx_game_state(mconfig, type, tag),
16 		m_cart(*this, "cartslot"),
17 		m_io_motionx(*this, "MOTIONX"),
18 		m_io_motiony(*this, "MOTIONY"),
19 		m_io_motionz(*this, "MOTIONZ"),
20 		m_cart_region(nullptr),
21 		m_ctrl_poll_timer(nullptr)
22 	{ }
23 
24 	void vii(machine_config &config);
25 
26 private:
27 	virtual void machine_start() override;
28 	virtual void machine_reset() override;
29 
30 	static const device_timer_id TIMER_CTRL_POLL = 0;
31 	virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override;
32 
33 	void vii_portb_w(uint16_t data);
34 
35 	DECLARE_DEVICE_IMAGE_LOAD_MEMBER(cart_load_vii);
36 
37 	virtual void poll_controls();
38 
39 	required_device<generic_slot_device> m_cart;
40 	required_ioport m_io_motionx;
41 	required_ioport m_io_motiony;
42 	required_ioport m_io_motionz;
43 	memory_region *m_cart_region;
44 
45 	emu_timer *m_ctrl_poll_timer;
46 	uint8_t m_controller_input[8];
47 };
48 
49 
device_timer(emu_timer & timer,device_timer_id id,int param,void * ptr)50 void vii_state::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
51 {
52 	switch (id)
53 	{
54 	case TIMER_CTRL_POLL:
55 		poll_controls();
56 		break;
57 	default:
58 		logerror("Unknown timer ID: %d\n", id);
59 		break;
60 	}
61 }
62 
vii_portb_w(uint16_t data)63 void vii_state::vii_portb_w(uint16_t data)
64 {
65 	switch_bank(((data & 0x80) >> 7) | ((data & 0x20) >> 4));
66 }
67 
68 
69 static INPUT_PORTS_START( vii )
70 	PORT_START("P1")
71 	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )    PORT_PLAYER(1) PORT_NAME("Joypad Up")
72 	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )  PORT_PLAYER(1) PORT_NAME("Joypad Down")
73 	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )  PORT_PLAYER(1) PORT_NAME("Joypad Left")
74 	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1) PORT_NAME("Joypad Right")
75 	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 )        PORT_PLAYER(1) PORT_NAME("Button A")
76 	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_BUTTON2 )        PORT_PLAYER(1) PORT_NAME("Button B")
77 	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_BUTTON3 )        PORT_PLAYER(1) PORT_NAME("Button C")
78 	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_BUTTON4 )        PORT_PLAYER(1) PORT_NAME("Button D")
79 
80 	PORT_START("MOTIONX")
81 	PORT_BIT( 0x3ff, 0x200, IPT_PADDLE ) PORT_MINMAX(0x000, 0x3ff) PORT_SENSITIVITY(50) PORT_KEYDELTA(50) PORT_NAME("Motion Control X")
82 
83 	PORT_START("MOTIONY")
84 	PORT_BIT( 0x3ff, 0x200, IPT_PADDLE ) PORT_MINMAX(0x000, 0x3ff) PORT_SENSITIVITY(50) PORT_KEYDELTA(50) PORT_NAME("Motion Control Y") PORT_PLAYER(2)
85 
86 	PORT_START("MOTIONZ")
87 	PORT_BIT( 0x3ff, 0x200, IPT_PADDLE ) PORT_MINMAX(0x000, 0x3ff) PORT_SENSITIVITY(50) PORT_KEYDELTA(50) PORT_NAME("Motion Control Z") PORT_PLAYER(3)
88 INPUT_PORTS_END
89 
machine_start()90 void vii_state::machine_start()
91 {
92 	spg2xx_game_state::machine_start();
93 
94 	// if there's a cart, override the standard banking
95 	if (m_cart && m_cart->exists())
96 	{
97 		std::string region_tag;
98 		m_cart_region = memregion(region_tag.assign(m_cart->tag()).append(GENERIC_ROM_REGION_TAG).c_str());
99 		m_bank->configure_entries(0, (m_cart_region->bytes() + 0x7fffff) / 0x800000, m_cart_region->base(), 0x800000);
100 		m_bank->set_entry(0);
101 	}
102 
103 	m_ctrl_poll_timer = timer_alloc(TIMER_CTRL_POLL);
104 	m_ctrl_poll_timer->adjust(attotime::never);
105 
106 	save_item(NAME(m_controller_input));
107 }
108 
machine_reset()109 void vii_state::machine_reset()
110 {
111 	spg2xx_game_state::machine_reset();
112 
113 	m_controller_input[0] = 0;
114 	m_controller_input[4] = 0;
115 	m_controller_input[6] = 0xff;
116 	m_controller_input[7] = 0;
117 
118 	m_ctrl_poll_timer->adjust(attotime::from_hz(60), 0, attotime::from_hz(60));
119 }
120 
121 
poll_controls()122 void vii_state::poll_controls()
123 {
124 	int32_t x = m_io_motionx ? ((int32_t)m_io_motionx->read() - 0x200) : 0;
125 	int32_t y = m_io_motiony ? ((int32_t)m_io_motiony->read() - 0x200) : 0;
126 	int32_t z = m_io_motionz ? ((int32_t)m_io_motionz->read() - 0x200) : 0;
127 
128 	uint8_t old_input[8];
129 	memcpy(old_input, m_controller_input, 8);
130 
131 	m_controller_input[0] = m_io_p1->read();
132 	m_controller_input[1] = (uint8_t)x;
133 	m_controller_input[2] = (uint8_t)y;
134 	m_controller_input[3] = (uint8_t)z;
135 	m_controller_input[4] = 0;
136 	x = (x >> 8) & 3;
137 	y = (y >> 8) & 3;
138 	z = (z >> 8) & 3;
139 	m_controller_input[5] = (z << 4) | (y << 2) | x;
140 	m_controller_input[6] = 0xff;
141 	m_controller_input[7] = 0;
142 
143 	if (memcmp(old_input, m_controller_input, 8))
144 	{
145 		for(int i = 0; i < 8; i++)
146 			m_maincpu->uart_rx(m_controller_input[i]);
147 	}
148 }
149 
DEVICE_IMAGE_LOAD_MEMBER(vii_state::cart_load_vii)150 DEVICE_IMAGE_LOAD_MEMBER(vii_state::cart_load_vii)
151 {
152 	uint32_t size = m_cart->common_get_size("rom");
153 
154 	if (size < 0x800000)
155 	{
156 		image.seterror(IMAGE_ERROR_UNSPECIFIED, "Unsupported cartridge size");
157 		return image_init_result::FAIL;
158 	}
159 
160 	m_cart->rom_alloc(size, GENERIC_ROM16_WIDTH, ENDIANNESS_LITTLE);
161 	m_cart->common_load_rom(m_cart->get_rom_base(), size, "rom");
162 
163 	return image_init_result::PASS;
164 }
165 
vii(machine_config & config)166 void vii_state::vii(machine_config &config)
167 {
168 	SPG24X(config, m_maincpu, XTAL(27'000'000), m_screen);
169 	m_maincpu->set_addrmap(AS_PROGRAM, &vii_state::mem_map_4m);
170 
171 	spg2xx_base(config);
172 
173 	m_maincpu->portb_out().set(FUNC(vii_state::vii_portb_w));
174 	m_maincpu->i2c_w().set(FUNC(vii_state::i2c_w));
175 	m_maincpu->i2c_r().set(FUNC(vii_state::i2c_r));
176 
177 	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "vii_cart");
178 	m_cart->set_width(GENERIC_ROM16_WIDTH);
179 	m_cart->set_device_load(FUNC(vii_state::cart_load_vii));
180 
181 	SOFTWARE_LIST(config, "vii_cart").set_original("vii");
182 }
183 
184 ROM_START( vii )
185 	ROM_REGION( 0x2000000, "maincpu", ROMREGION_ERASE00 )
186 	ROM_LOAD16_WORD_SWAP( "vii.bin", 0x0000, 0x2000000, CRC(04627639) SHA1(f883a92d31b53c9a5b0cdb112d07cd793c95fc43))
187 ROM_END
188 
189 // Jungle Soft TV games
190 CONS( 2007, vii,      0, 0, vii,        vii,      vii_state,         empty_init,      "Jungle Soft / KenSingTon / Siatronics",       "Vii",         MACHINE_IMPERFECT_SOUND | MACHINE_IMPERFECT_GRAPHICS ) // motion controls are awkward, but playable for the most part
191