1 // license:LGPL-2.1+ 2 // copyright-holders:David Haywood, Angelo Salese, ElSemi, Andrew Gardner 3 #ifndef MAME_INCLUDES_HNG64_H 4 #define MAME_INCLUDES_HNG64_H 5 6 #pragma once 7 8 #include "machine/msm6242.h" 9 #include "machine/timer.h" 10 #include "cpu/mips/mips3.h" 11 #include "cpu/nec/v5x.h" 12 #include "sound/l7a1045_l6028_dsp_a.h" 13 #include "video/poly.h" 14 #include "cpu/tlcs870/tlcs870.h" 15 #include "machine/mb8421.h" 16 #include "emupal.h" 17 #include "screen.h" 18 #include "tilemap.h" 19 20 enum hng64trans_t 21 { 22 HNG64_TILEMAP_NORMAL = 1, 23 HNG64_TILEMAP_ADDITIVE, 24 HNG64_TILEMAP_ALPHA 25 }; 26 27 struct blit_parameters 28 { 29 bitmap_rgb32 * bitmap; 30 rectangle cliprect; 31 uint32_t tilemap_priority_code; 32 uint8_t mask; 33 uint8_t value; 34 uint8_t alpha; 35 hng64trans_t drawformat; 36 }; 37 38 #define HNG64_MASTER_CLOCK 50000000 39 40 41 ///////////////// 42 /// 3d Engine /// 43 ///////////////// 44 45 struct polyVert 46 { 47 float worldCoords[4]; // World space coordinates (X Y Z 1.0) 48 49 float texCoords[4]; // Texture coordinates (U V 0 1.0) -> OpenGL style... 50 51 float normal[4]; // Normal (X Y Z 1.0) 52 float clipCoords[4]; // Homogeneous screen space coordinates (X Y Z W) 53 54 float light[3]; // The intensity of the illumination at this point 55 }; 56 57 struct polygon 58 { 59 int n; // Number of sides 60 polyVert vert[10]; // Vertices (maximum number per polygon is 10 -> 3+6) 61 62 float faceNormal[4]; // Normal of the face overall - for calculating visibility and flat-shading... 63 int visible; // Polygon visibility in scene 64 65 uint8_t texIndex; // Which texture to draw from (0x00-0x0f) 66 uint8_t texType; // How to index into the texture 67 uint8_t texPageSmall; // Does this polygon use 'small' texture pages? 68 uint8_t texPageHorizOffset; // If it does use small texture pages, how far is this page horizontally offset? 69 uint8_t texPageVertOffset; // If it does use small texture pages, how far is this page vertically offset? 70 71 uint32_t palOffset; // The base offset where this object's palette starts. 72 uint32_t palPageSize; // The size of the palette page that is being pointed to. 73 74 uint32_t debugColor; // Will go away someday. Used to explicitly color polygons for debugging. 75 }; 76 77 78 ///////////////////////// 79 /// polygon rendering /// 80 ///////////////////////// 81 82 // Refer to the clipping planes as numbers 83 #define HNG64_LEFT 0 84 #define HNG64_RIGHT 1 85 #define HNG64_TOP 2 86 #define HNG64_BOTTOM 3 87 #define HNG64_NEAR 4 88 #define HNG64_FAR 5 89 90 91 //////////////////////////////////// 92 /// Polygon rasterizer interface /// 93 //////////////////////////////////// 94 95 const int HNG64_MAX_POLYGONS = 10000; 96 97 typedef frustum_clip_vertex<float, 5> hng64_clip_vertex; 98 99 struct hng64_poly_data 100 { 101 uint8_t texType; 102 uint8_t texIndex; 103 uint8_t texPageSmall; 104 uint8_t texPageHorizOffset; 105 uint8_t texPageVertOffset; 106 int palOffset; 107 int palPageSize; 108 int debugColor; 109 }; 110 111 class hng64_state; 112 113 class hng64_poly_renderer : public poly_manager<float, hng64_poly_data, 7, HNG64_MAX_POLYGONS> 114 { 115 public: 116 hng64_poly_renderer(hng64_state& state); 117 118 void drawShaded(polygon *p); 119 void render_scanline(int32_t scanline, const extent_t& extent, const hng64_poly_data& renderData, int threadid); 120 state()121 hng64_state& state() { return m_state; } colorBuffer3d()122 bitmap_rgb32& colorBuffer3d() { return m_colorBuffer3d; } depthBuffer3d()123 float* depthBuffer3d() { return m_depthBuffer3d.get(); } 124 125 private: 126 hng64_state& m_state; 127 128 // (Temporarily class members - someday they will live in the memory map) 129 bitmap_rgb32 m_colorBuffer3d; 130 std::unique_ptr<float[]> m_depthBuffer3d; 131 }; 132 133 134 // TODO, this could become the IO board device emulation 135 class hng64_lamps_device : public device_t 136 { 137 public: 138 hng64_lamps_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock); 139 lamps0_out_cb()140 auto lamps0_out_cb() { return m_lamps_out_cb[0].bind(); } lamps1_out_cb()141 auto lamps1_out_cb() { return m_lamps_out_cb[1].bind(); } lamps2_out_cb()142 auto lamps2_out_cb() { return m_lamps_out_cb[2].bind(); } lamps3_out_cb()143 auto lamps3_out_cb() { return m_lamps_out_cb[3].bind(); } lamps4_out_cb()144 auto lamps4_out_cb() { return m_lamps_out_cb[4].bind(); } lamps5_out_cb()145 auto lamps5_out_cb() { return m_lamps_out_cb[5].bind(); } lamps6_out_cb()146 auto lamps6_out_cb() { return m_lamps_out_cb[6].bind(); } lamps7_out_cb()147 auto lamps7_out_cb() { return m_lamps_out_cb[7].bind(); } 148 lamps_w(offs_t offset,uint8_t data)149 void lamps_w(offs_t offset, uint8_t data) { m_lamps_out_cb[offset](data); } 150 151 protected: 152 virtual void device_start() override; 153 154 private: 155 devcb_write8::array<8> m_lamps_out_cb; 156 }; 157 158 159 class hng64_state : public driver_device 160 { 161 public: hng64_state(const machine_config & mconfig,device_type type,const char * tag)162 hng64_state(const machine_config &mconfig, device_type type, const char *tag) : 163 driver_device(mconfig, type, tag), 164 m_screen(*this, "screen"), 165 m_palette(*this, "palette"), 166 m_maincpu(*this, "maincpu"), 167 m_audiocpu(*this, "audiocpu"), 168 m_iomcu(*this, "iomcu"), 169 m_lamps(*this, "lamps"), 170 m_dt71321_dpram(*this, "dt71321_dpram"), 171 m_dsp(*this, "l7a1045"), 172 m_comm(*this, "network"), 173 m_rtc(*this, "rtc"), 174 m_mainram(*this, "mainram"), 175 m_cart(*this, "cart"), 176 m_sysregs(*this, "sysregs"), 177 m_rombase(*this, "rombase"), 178 m_spriteram(*this, "spriteram"), 179 m_spriteregs(*this, "spriteregs"), 180 m_videoram(*this, "videoram"), 181 m_videoregs(*this, "videoregs"), 182 m_tcram(*this, "tcram"), 183 m_fbtable(*this, "fbtable"), 184 m_comhack(*this, "comhack"), 185 m_fbram1(*this, "fbram1"), 186 m_fbram2(*this, "fbram2"), 187 m_idt7133_dpram(*this, "com_ram"), 188 m_gfxdecode(*this, "gfxdecode"), 189 m_in(*this, "IN%u", 0U), 190 m_an_in(*this, "AN%u", 0U), 191 m_samsho64_3d_hack(0), 192 m_roadedge_3d_hack(0) 193 { } 194 195 void hng64(machine_config &config); 196 void hng64_default(machine_config &config); 197 void hng64_drive(machine_config &config); 198 void hng64_shoot(machine_config &config); 199 void hng64_fight(machine_config &config); 200 201 void init_roadedge(); 202 void init_hng64_drive(); 203 void init_hng64(); 204 void init_hng64_shoot(); 205 void init_ss64(); 206 void init_hng64_fght(); 207 208 uint8_t *m_texturerom; 209 required_device<screen_device> m_screen; 210 required_device<palette_device> m_palette; 211 212 private: 213 /* TODO: NOT measured! */ 214 const int PIXEL_CLOCK = (HNG64_MASTER_CLOCK*2)/4; // x 2 is due to the interlaced screen ... 215 216 const int HTOTAL = 0x200+0x100; 217 const int HBEND = 0; 218 const int HBSTART = 0x200; 219 220 const int VTOTAL = 264*2; 221 const int VBEND = 0; 222 const int VBSTART = 224*2; 223 224 225 required_device<mips3_device> m_maincpu; 226 required_device<v53a_device> m_audiocpu; 227 required_device<tmp87ph40an_device> m_iomcu; 228 required_device<hng64_lamps_device> m_lamps; 229 required_device<idt71321_device> m_dt71321_dpram; 230 required_device<l7a1045_sound_device> m_dsp; 231 required_device<cpu_device> m_comm; 232 required_device<msm6242_device> m_rtc; 233 234 required_shared_ptr<uint32_t> m_mainram; 235 required_shared_ptr<uint32_t> m_cart; 236 required_shared_ptr<uint32_t> m_sysregs; 237 required_shared_ptr<uint32_t> m_rombase; 238 required_shared_ptr<uint32_t> m_spriteram; 239 required_shared_ptr<uint32_t> m_spriteregs; 240 required_shared_ptr<uint32_t> m_videoram; 241 required_shared_ptr<uint32_t> m_videoregs; 242 required_shared_ptr<uint32_t> m_tcram; 243 244 std::unique_ptr<uint16_t[]> m_dl; 245 required_shared_ptr<uint32_t> m_fbtable; 246 required_shared_ptr<uint32_t> m_comhack; 247 required_shared_ptr<uint32_t> m_fbram1; 248 required_shared_ptr<uint32_t> m_fbram2; 249 250 required_shared_ptr<uint32_t> m_idt7133_dpram; 251 //required_shared_ptr<uint8_t> m_com_mmu_mem; 252 253 required_device<gfxdecode_device> m_gfxdecode; 254 255 required_ioport_array<8> m_in; 256 required_ioport_array<8> m_an_in; 257 258 hng64_default_lamps0_w(uint8_t data)259 void hng64_default_lamps0_w(uint8_t data) { logerror("lamps0 %02x\n", data); } hng64_default_lamps1_w(uint8_t data)260 void hng64_default_lamps1_w(uint8_t data) { logerror("lamps1 %02x\n", data); } hng64_default_lamps2_w(uint8_t data)261 void hng64_default_lamps2_w(uint8_t data) { logerror("lamps2 %02x\n", data); } hng64_default_lamps3_w(uint8_t data)262 void hng64_default_lamps3_w(uint8_t data) { logerror("lamps3 %02x\n", data); } hng64_default_lamps4_w(uint8_t data)263 void hng64_default_lamps4_w(uint8_t data) { logerror("lamps4 %02x\n", data); } hng64_default_lamps5_w(uint8_t data)264 void hng64_default_lamps5_w(uint8_t data) { logerror("lamps5 %02x\n", data); } hng64_default_lamps6_w(uint8_t data)265 void hng64_default_lamps6_w(uint8_t data) { logerror("lamps6 %02x\n", data); } hng64_default_lamps7_w(uint8_t data)266 void hng64_default_lamps7_w(uint8_t data) { logerror("lamps7 %02x\n", data); } 267 268 void hng64_drive_lamps7_w(uint8_t data); 269 void hng64_drive_lamps6_w(uint8_t data); 270 void hng64_drive_lamps5_w(uint8_t data); 271 272 void hng64_shoot_lamps7_w(uint8_t data); 273 void hng64_shoot_lamps6_w(uint8_t data); 274 275 void hng64_fight_lamps6_w(uint8_t data); 276 277 int m_samsho64_3d_hack; 278 int m_roadedge_3d_hack; 279 280 uint8_t m_fbcontrol[4]; 281 282 std::unique_ptr<uint16_t[]> m_soundram; 283 std::unique_ptr<uint16_t[]> m_soundram2; 284 285 /* Communications stuff */ 286 std::unique_ptr<uint8_t[]> m_com_op_base; 287 std::unique_ptr<uint8_t[]> m_com_virtual_mem; 288 uint8_t m_com_shared[8]; 289 290 int32_t m_dma_start; 291 int32_t m_dma_dst; 292 int32_t m_dma_len; 293 294 uint16_t m_mcu_en; 295 296 uint32_t m_activeDisplayList; 297 uint32_t m_no_machine_error_code; 298 299 uint32_t m_unk_vreg_toggle; 300 uint32_t m_p1_trig; 301 302 //uint32_t *q2; 303 304 uint8_t m_screen_dis; 305 306 struct hng64_tilemap { 307 tilemap_t *m_tilemap_8x8; 308 tilemap_t *m_tilemap_16x16; 309 tilemap_t *m_tilemap_16x16_alt; 310 }; 311 312 hng64_tilemap m_tilemap[4]; 313 314 uint8_t m_additive_tilemap_debug; 315 316 uint32_t m_old_animmask; 317 uint32_t m_old_animbits; 318 uint16_t m_old_tileflags[4]; 319 320 // 3d State 321 int m_paletteState3d; 322 float m_projectionMatrix[16]; 323 float m_modelViewMatrix[16]; 324 float m_cameraMatrix[16]; 325 326 float m_lightStrength; 327 float m_lightVector[3]; 328 329 uint32_t hng64_com_r(offs_t offset); 330 void hng64_com_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 331 void hng64_com_share_w(offs_t offset, uint8_t data); 332 uint8_t hng64_com_share_r(offs_t offset); 333 void hng64_com_share_mips_w(offs_t offset, uint8_t data); 334 uint8_t hng64_com_share_mips_r(offs_t offset); 335 uint32_t hng64_sysregs_r(offs_t offset, uint32_t mem_mask = ~0); 336 void hng64_sysregs_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 337 uint32_t hng64_rtc_r(offs_t offset, uint32_t mem_mask = ~0); 338 void hng64_rtc_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 339 uint32_t hng64_dmac_r(offs_t offset, uint32_t mem_mask = ~0); 340 void hng64_dmac_w(address_space &space, offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 341 uint32_t hng64_irqc_r(offs_t offset, uint32_t mem_mask = ~0); 342 void hng64_irqc_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 343 void hng64_mips_to_iomcu_irq_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 344 345 uint8_t hng64_dualport_r(offs_t offset); 346 void hng64_dualport_w(offs_t offset, uint8_t data); 347 348 uint8_t hng64_fbcontrol_r(offs_t offset); 349 void hng64_fbcontrol_w(offs_t offset, uint8_t data); 350 351 void hng64_fbunkpair_w(offs_t offset, uint16_t data); 352 void hng64_fbscroll_w(offs_t offset, uint16_t data); 353 354 void hng64_fbunkbyte_w(offs_t offset, uint8_t data); 355 356 uint32_t hng64_fbtable_r(offs_t offset, uint32_t mem_mask = ~0); 357 void hng64_fbtable_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 358 359 uint32_t hng64_fbram1_r(offs_t offset); 360 void hng64_fbram1_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 361 362 uint32_t hng64_fbram2_r(offs_t offset); 363 void hng64_fbram2_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 364 365 void dl_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 366 //uint32_t dl_r(); 367 void dl_control_w(uint32_t data); 368 void dl_upload_w(uint32_t data); 369 void dl_unk_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 370 uint32_t dl_vreg_r(); 371 372 void tcram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 373 uint32_t tcram_r(offs_t offset); 374 375 void hng64_soundram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 376 uint32_t hng64_soundram_r(offs_t offset); 377 void hng64_vregs_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 378 379 // not actually used, but left in code so you can turn it and see the (possibly undesired?) behavior, see notes in memory map 380 void hng64_soundram2_w(uint32_t data); 381 uint32_t hng64_soundram2_r(); 382 383 void hng64_soundcpu_enable_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 384 385 void hng64_sprite_clear_even_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 386 void hng64_sprite_clear_odd_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 387 void hng64_videoram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0); 388 389 // shared ram access 390 uint8_t ioport0_r(); 391 void ioport0_w(uint8_t data); 392 void ioport7_w(uint8_t data); 393 394 // input port access 395 uint8_t ioport3_r(); 396 void ioport3_w(uint8_t data); 397 void ioport1_w(uint8_t data); 398 399 // unknown access 400 void ioport4_w(uint8_t data); 401 402 // analog input access 403 uint8_t anport0_r(); 404 uint8_t anport1_r(); 405 uint8_t anport2_r(); 406 uint8_t anport3_r(); 407 uint8_t anport4_r(); 408 uint8_t anport5_r(); 409 uint8_t anport6_r(); 410 uint8_t anport7_r(); 411 412 DECLARE_WRITE_LINE_MEMBER( sio0_w ); 413 414 uint8_t m_port7; 415 uint8_t m_port1; 416 417 int m_ex_ramaddr; 418 int m_ex_ramaddr_upper; 419 420 TIMER_CALLBACK_MEMBER(tempio_irqon_callback); 421 TIMER_CALLBACK_MEMBER(tempio_irqoff_callback); 422 emu_timer *m_tempio_irqon_timer; 423 emu_timer *m_tempio_irqoff_timer; 424 void init_io(); 425 426 void init_hng64_reorder_gfx(); 427 428 void set_irq(uint32_t irq_vector); 429 uint32_t m_irq_pending; 430 431 TIMER_CALLBACK_MEMBER(comhack_callback); 432 emu_timer *m_comhack_timer; 433 434 435 int m_irq_level; 436 TILE_GET_INFO_MEMBER(get_hng64_tile0_8x8_info); 437 TILE_GET_INFO_MEMBER(get_hng64_tile0_16x16_info); 438 TILE_GET_INFO_MEMBER(get_hng64_tile1_8x8_info); 439 TILE_GET_INFO_MEMBER(get_hng64_tile1_16x16_info); 440 TILE_GET_INFO_MEMBER(get_hng64_tile2_8x8_info); 441 TILE_GET_INFO_MEMBER(get_hng64_tile2_16x16_info); 442 TILE_GET_INFO_MEMBER(get_hng64_tile3_8x8_info); 443 TILE_GET_INFO_MEMBER(get_hng64_tile3_16x16_info); 444 virtual void machine_start() override; 445 virtual void machine_reset() override; 446 virtual void video_start() override; 447 uint32_t screen_update_hng64(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect); 448 DECLARE_WRITE_LINE_MEMBER(screen_vblank_hng64); 449 TIMER_DEVICE_CALLBACK_MEMBER(hng64_irq); 450 void do_dma(address_space &space); 451 452 void hng64_mark_all_tiles_dirty(int tilemap); 453 void hng64_mark_tile_dirty(int tilemap, int tile_index); 454 void hng64_drawtilemap(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect, int tm); 455 456 void hng64_tilemap_draw_roz_core(screen_device &screen, tilemap_t *tmap, const blit_parameters *blit, 457 uint32_t startx, uint32_t starty, int incxx, int incxy, int incyx, int incyy, int wraparound); 458 459 void hng64_tilemap_draw_roz(screen_device &screen, bitmap_rgb32 &dest, const rectangle &cliprect, tilemap_t *tmap, 460 uint32_t startx, uint32_t starty, int incxx, int incxy, int incyx, int incyy, 461 int wraparound, uint32_t flags, uint8_t priority, hng64trans_t drawformat); 462 463 void hng64_tilemap_draw_roz_primask(screen_device &screen, bitmap_rgb32 &dest, const rectangle &cliprect, tilemap_t *tmap, 464 uint32_t startx, uint32_t starty, int incxx, int incxy, int incyx, int incyy, 465 int wraparound, uint32_t flags, uint8_t priority, uint8_t priority_mask, hng64trans_t drawformat); 466 467 468 469 470 std::unique_ptr<hng64_poly_renderer> m_poly_renderer; 471 472 TIMER_CALLBACK_MEMBER(hng64_3dfifo_processed); 473 emu_timer *m_3dfifo_timer; 474 475 uint16_t* m_vertsrom; 476 int m_vertsrom_size; 477 std::vector<polygon> m_polys; // HNG64_MAX_POLYGONS 478 479 void clear3d(); 480 void hng64_command3d(const uint16_t* packet); 481 void draw_sprites(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect); 482 void transition_control(bitmap_rgb32 &bitmap, const rectangle &cliprect); 483 void setCameraTransformation(const uint16_t* packet); 484 void setLighting(const uint16_t* packet); 485 void set3dFlags(const uint16_t* packet); 486 void setCameraProjectionMatrix(const uint16_t* packet); 487 void recoverPolygonBlock(const uint16_t* packet, int& numPolys); 488 void printPacket(const uint16_t* packet, int hex); 489 float uToF(uint16_t input); 490 void matmul4(float *product, const float *a, const float *b); 491 void vecmatmul4(float *product, const float *a, const float *b); 492 float vecDotProduct(const float *a, const float *b); 493 void setIdentity(float *matrix); 494 void normalize(float* x); 495 496 void reset_sound(); 497 void reset_net(); 498 499 DECLARE_WRITE_LINE_MEMBER(dma_hreq_cb); 500 uint8_t dma_memr_cb(offs_t offset); 501 void dma_iow3_cb(uint8_t data); 502 DECLARE_WRITE_LINE_MEMBER(tcu_tm0_cb); 503 DECLARE_WRITE_LINE_MEMBER(tcu_tm1_cb); 504 DECLARE_WRITE_LINE_MEMBER(tcu_tm2_cb); 505 506 507 508 uint16_t hng64_sound_port_0008_r(offs_t offset, uint16_t mem_mask = ~0); 509 void hng64_sound_port_0008_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 510 511 void hng64_sound_port_000a_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 512 void hng64_sound_port_000c_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 513 514 void hng64_sound_port_0080_w(uint16_t data); 515 516 void hng64_sound_bank_w(offs_t offset, uint16_t data); 517 uint16_t main_sound_comms_r(offs_t offset); 518 void main_sound_comms_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 519 uint16_t sound_comms_r(offs_t offset); 520 void sound_comms_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0); 521 uint16_t main_latch[2]; 522 uint16_t sound_latch[2]; 523 void hng64_audio(machine_config &config); 524 void hng64_network(machine_config &config); 525 void hng_comm_io_map(address_map &map); 526 void hng_comm_map(address_map &map); 527 void hng_map(address_map &map); 528 void hng_sound_io(address_map &map); 529 void hng_sound_map(address_map &map); 530 }; 531 532 #endif // MAME_INCLUDES_HNG64_H 533