Previous 15994 Revisions Next

r14030 Sunday 15th January, 2012 at 01:19:19 UTC by MooglyGuy
[N64] Various changes: [MooglyGuy, Happy]
- Converted AI / VI / MI / RI / SI / PI into a modernized device
- PI DMA now takes place after an appropriate delay to simulate transfer time
- SP DMA no longer rejects transfers of 0 bytes (should transfer one 8-byte word)
[src/mame/includes]n64.h
[src/mame/machine]n64.c
[src/mame/video]n64.c n64.h
[src/mess/drivers]n64.c

src/mame/machine/n64.c
r14029r14030
44#include "cpu/mips/mips3.h"
55#include "cpu/mips/mips3com.h"
66#include "includes/n64.h"
7#include "sound/dmadac.h"
87#include "profiler.h"
98
109UINT32 *rdram;
1110UINT32 *rsp_imem;
1211UINT32 *rsp_dmem;
1312
14// Memory Interface
15static UINT32 mi_version = 0;
16static UINT32 mi_interrupt = 0;
17static UINT32 mi_intr_mask = 0;
18static UINT32 mi_mode = 0;
13// device type definition
14const device_type N64PERIPH = &device_creator<n64_periphs>;
1915
16static TIMER_CALLBACK(ai_timer_callback);
17static TIMER_CALLBACK(pi_dma_callback);
18
19n64_periphs::n64_periphs(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
20    : device_t(mconfig, N64PERIPH, "N64 Periphal Chips", tag, owner, clock)
21{
22}
23
24
25void n64_periphs::device_start()
26{
27   ai_timer = machine().scheduler().timer_alloc(FUNC(ai_timer_callback));
28   pi_dma_timer = machine().scheduler().timer_alloc(FUNC(pi_dma_callback));
29}
30
31void n64_periphs::device_reset()
32{
33   UINT32 *cart = (UINT32*)machine().region("user2")->base();
34
35   maincpu = machine().device("maincpu");
36   rspcpu = machine().device("rsp");
37   mem_map = maincpu->memory().space(AS_PROGRAM);
38
39   mi_version = 0;
40   mi_interrupt = 0;
41   mi_intr_mask = 0;
42   mi_mode = 0;
43
44   sp_mem_addr = 0;
45   sp_dram_addr = 0;
46   sp_dma_length = 0;
47   sp_dma_count = 0;
48   sp_dma_skip = 0;
49   sp_semaphore = 0;
50
51   vi_width = 0;
52   vi_origin = 0;
53   vi_control = 0;
54   vi_burst = 0;
55   vi_vsync = 0;
56   vi_hsync = 0;
57   vi_leap = 0;
58   vi_hstart = 0;
59   vi_vstart = 0;
60   vi_intr = 0;
61   vi_vburst = 0;
62   vi_xscale = 0;
63   vi_yscale = 0;
64
65   ai_dac[0] = machine().device<dmadac_sound_device>("dac1");
66   ai_dac[1] = machine().device<dmadac_sound_device>("dac2");
67   ai_timer->adjust(attotime::never);
68   memset(ai_fifo, 0, sizeof(ai_fifo));
69   ai_fifo_wpos = 0;
70   ai_fifo_rpos = 0;
71   ai_fifo_num = 0;
72   ai_dram_addr = 0;
73   ai_len = 0 ;
74   ai_control = 0;
75   ai_dacrate = 0;
76   ai_bitrate = 0;
77   ai_status = 0;
78
79   pi_dma_timer->adjust(attotime::never);
80   pi_first_dma = 1;
81   pi_rd_len = 0;
82   pi_wr_len = 0;
83   pi_status = 0;
84   pi_bsd_dom1_lat = 0;
85   pi_bsd_dom1_pwd = 0;
86   pi_bsd_dom1_pgs = 0;
87   pi_bsd_dom1_rls = 0;
88   pi_bsd_dom2_lat = 0;
89   pi_bsd_dom2_pwd = 0;
90   pi_bsd_dom2_pgs = 0;
91   pi_bsd_dom2_rls = 0;
92   pi_dma_dir = 0;
93
94   memset(pif_ram, 0, sizeof(pif_ram));
95   memset(pif_cmd, 0, sizeof(pif_cmd));
96   si_dram_addr = 0;
97   si_pif_addr = 0;
98   si_status = 0;
99
100   memset(eeprom, 0, sizeof(eeprom));
101   memset(mempack, 0, sizeof(mempack));
102
103   dp_clock = 0;
104
105   cic_status = 0;
106
107    // bootcode differs between CIC-chips, so we can use its checksum to detect the CIC-chip
108    UINT64 boot_checksum = 0;
109    for(int i = 0x40; i < 0x1000; i+=4)
110    {
111        boot_checksum += cart[i/4]+i;
112    }
113
114    if (boot_checksum == U64(0x000000d057e84864))
115    {
116        // CIC-NUS-6101
117        printf("CIC-NUS-6102 detected\n");
118        pif_ram[0x24] = 0x00;
119        pif_ram[0x25] = 0x02;
120        pif_ram[0x26] = 0x3f;
121        pif_ram[0x27] = 0x3f;
122        // crc_seed = 0x3f;
123    }
124    else if (boot_checksum == U64(0x000000cffb830843) || boot_checksum == U64(0x000000d0027fdf31))
125    {
126        // CIC-NUS-6103
127        printf("CIC-NUS-6101 detected\n");
128        // crc_seed = 0x78;
129        pif_ram[0x24] = 0x00;
130        pif_ram[0x25] = 0x06;
131        pif_ram[0x26] = 0x3f;
132        pif_ram[0x27] = 0x3f;
133    }
134    else if (boot_checksum == U64(0x000000d6499e376b))
135    {
136        // CIC-NUS-6103
137        printf("CIC-NUS-6103 detected\n");
138        // crc_seed = 0x78;
139        pif_ram[0x24] = 0x00;
140        pif_ram[0x25] = 0x02;
141        pif_ram[0x26] = 0x78;
142        pif_ram[0x27] = 0x3f;
143    }
144    else if (boot_checksum == U64(0x0000011a4a1604b6))
145    {
146        // CIC-NUS-6105
147        printf("CIC-NUS-6105 detected\n");
148        // crc_seed = 0x91;
149
150        // first_rsp = 0;
151        pif_ram[0x24] = 0x00;
152        pif_ram[0x25] = 0x02;
153        pif_ram[0x26] = 0x91;
154        pif_ram[0x27] = 0x3f;
155    }
156    else if (boot_checksum == U64(0x000000d6d5de4ba0))
157    {
158        // CIC-NUS-6106
159        printf("CIC-NUS-6106 detected\n");
160        // crc_seed = 0x85;
161        pif_ram[0x24] = 0x00;
162        pif_ram[0x25] = 0x02;
163        pif_ram[0x26] = 0x85;
164        pif_ram[0x27] = 0x3f;
165    }
166    else
167    {
168        printf("Unknown BootCode Checksum %08X%08X\n", (UINT32)(boot_checksum>>32),(UINT32)(boot_checksum));
169    }
170}
171
20172// Memory Interface (MI)
21173
22174#define MI_CLR_INIT             0x0080      /* Bit  7: clear init mode */
r14029r14030
30182#define MI_MODE_EBUS            0x0100      /* Bit  8: ebus test mode */
31183#define MI_MODE_RDRAM           0x0200      /* Bit  9: RDRAM reg mode */
32184
33READ32_HANDLER( n64_mi_reg_r )
185READ32_MEMBER( n64_periphs::mi_reg_r )
34186{
187   //printf("mi_reg_r %08x\n", offset * 4);
35188   switch (offset)
36189   {
37190        case 0x00/4:            // MI_MODE_REG
r14029r14030
47200         return mi_intr_mask;
48201
49202      default:
50         logerror("mi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
203         logerror("mi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&mem_map->device()));
51204         break;
52205   }
53206
54207   return 0;
55208}
56209
57WRITE32_HANDLER( n64_mi_reg_w )
210WRITE32_MEMBER( n64_periphs::mi_reg_w )
58211{
212   //printf("mi_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
59213   switch (offset)
60214   {
61215      case 0x00/4:      // MI_INIT_MODE_REG
r14029r14030
67221            if (data & MI_SET_RDRAM)    mi_mode |=  MI_MODE_RDRAM;
68222            if (data & MI_CLR_DP_INTR)
69223         {
70            clear_rcp_interrupt(space->machine(), DP_INTERRUPT);
224            clear_rcp_interrupt(DP_INTERRUPT);
71225         }
72226         break;
73227
r14029r14030
129283      }
130284
131285      default:
132         logerror("mi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
286         logerror("mi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
133287         break;
134288   }
135289}
136290
137static dmadac_sound_device *dmadac[2];
138
139291void signal_rcp_interrupt(running_machine &machine, int interrupt)
140292{
293   machine.device<n64_periphs>("rcp")->signal_rcp_interrupt(interrupt);
294}
295
296void n64_periphs::signal_rcp_interrupt(int interrupt)
297{
141298   if (mi_intr_mask & interrupt)
142299   {
143300      mi_interrupt |= interrupt;
144301
145      cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, ASSERT_LINE);
302      cputag_set_input_line(machine(), "maincpu", INPUT_LINE_IRQ0, ASSERT_LINE);
146303   }
147304}
148305
149void clear_rcp_interrupt(running_machine &machine, int interrupt)
306void n64_periphs::clear_rcp_interrupt(int interrupt)
150307{
151308   mi_interrupt &= ~interrupt;
152309
153310   //if (!mi_interrupt)
154311   {
155      cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, CLEAR_LINE);
312      cputag_set_input_line(machine(), "maincpu", INPUT_LINE_IRQ0, CLEAR_LINE);
156313   }
157314}
158315
159static UINT8 is64_buffer[0x10000];
160
161READ32_HANDLER( n64_is64_r )
316READ32_MEMBER( n64_periphs::is64_r )
162317{
163318    switch(offset)
164319    {
r14029r14030
182337    }
183338}
184339
185WRITE32_HANDLER( n64_is64_w )
340WRITE32_MEMBER( n64_periphs::is64_w )
186341{
187342    int i = 0;
188343
r14029r14030
209364    }
210365}
211366
212READ32_HANDLER( n64_open_r )
367READ32_MEMBER( n64_periphs::open_r )
213368{
214369    UINT32 retval = (offset << 2) & 0x0000ffff;
215370    retval = (retval << 16) | retval;
216371    return retval;
217372}
218373
219WRITE32_HANDLER( n64_open_w )
374WRITE32_MEMBER( n64_periphs::open_w )
220375{
221376    // Do nothing
222377}
223378
224// RDRAM registers
225static UINT32 rdram_config;
226static UINT32 rdram_device_id;
227static UINT32 rdram_delay;
228static UINT32 rdram_mode;
229static UINT32 rdram_ref_interval;
230static UINT32 rdram_ref_row;
231static UINT32 rdram_ras_interval;
232static UINT32 rdram_min_interval;
233static UINT32 rdram_addr_select;
234static UINT32 rdram_device_manuf;
379// RDRAM Interface (RI)
235380
236READ32_HANDLER( n64_rdram_reg_r )
237{
238    switch (offset)
239    {
240        case 0x00/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
241            return rdram_config;
381#define RDRAM_CONFIG       (0)
382#define RDRAM_DEVICE_ID    (1)
383#define RDRAM_DELAY         (2)
384#define RDRAM_MODE         (3)
385#define RDRAM_REF_INTERVAL   (4)
386#define RDRAM_REF_ROW      (5)
387#define RDRAM_RAS_INTERVAL   (6)
388#define RDRAM_MIN_INTERVAL   (7)
389#define RDRAM_ADDR_SELECT   (8)
390#define RDRAM_DEVICE_MANUF   (9)
242391
243        case 0x04/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
244            return rdram_device_id;
245
246        case 0x08/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
247            return rdram_delay;
248
249        case 0x0c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
250            return rdram_mode;
251
252        case 0x10/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
253            return rdram_ref_interval;
254
255        case 0x14/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
256            return rdram_ref_row;
257
258        case 0x18/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
259            return rdram_ras_interval;
260
261        case 0x1c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
262            return rdram_min_interval;
263
264        case 0x20/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
265            return rdram_addr_select;
266
267        case 0x24/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
268            return rdram_device_manuf;
269
270        default:
271            logerror("rdram_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
272            break;
273    }
274    return 0;
275}
276
277WRITE32_HANDLER( n64_rdram_reg_w )
392READ32_MEMBER( n64_periphs::rdram_reg_r )
278393{
279    switch (offset)
280    {
281        case 0x00/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
282            rdram_config = data;
283            break;
284
285        case 0x04/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
286            rdram_device_id = data;
287            break;
288
289        case 0x08/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
290            rdram_delay = data;
291            break;
292
293        case 0x0c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
294            rdram_mode = data;
295            break;
296
297        case 0x10/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
298            rdram_ref_interval = data;
299            break;
300
301        case 0x14/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
302            rdram_ref_row = data;
303            break;
304
305        case 0x18/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
306            rdram_ras_interval = data;
307            break;
308
309        case 0x1c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
310            rdram_min_interval = data;
311            break;
312
313        case 0x20/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
314            rdram_addr_select = data;
315            break;
316
317        case 0x24/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
318            rdram_device_manuf = data;
319            break;
320
321        default:
322            logerror("mi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
323            break;
324    }
394   //printf("rdram_reg_r %08x\n", offset * 4);
395   if(offset > 0x24/4)
396   {
397      logerror("rdram_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(maincpu));
398      return 0;
399   }
400   return rdram_regs[offset];
325401}
326402
327// RSP Interface
328
329static UINT32 sp_mem_addr;
330static UINT32 sp_dram_addr;
331static int sp_dma_length;
332static int sp_dma_count;
333static int sp_dma_skip;
334static UINT32 sp_semaphore;
335static UINT32 dp_clock = 0;
336
337static void sp_dma(running_machine &machine, int direction)
403WRITE32_MEMBER( n64_periphs::rdram_reg_w )
338404{
339   address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
340   int i, c;
341
342   if (sp_dma_length == 0)
405   //printf("rdram_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
406   if(offset > 0x24/4)
343407   {
408      logerror("mi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
344409      return;
345410   }
411   COMBINE_DATA(&rdram_regs[offset]);
412}
346413
414// RSP Interface
415
416void n64_periphs::sp_dma(int direction)
417{
347418   sp_dma_length++;
348419   if ((sp_dma_length & 7) != 0)
349420   {
350      //fatalerror("sp_dma (%s): sp_dma_length unaligned %08X\n", cpu ? "RSP" : "R4300i", sp_dma_length);
351      //sp_dma_length = sp_dma_length & ~3;
352421        sp_dma_length = (sp_dma_length + 7) & ~7;
353
354      //sp_dma_length &= ~7;
355422   }
356423
357424   if (sp_mem_addr & 0x3)
358425   {
359        //sp_mem_addr = (sp_mem_addr + 3) & ~3;
360426        sp_mem_addr = sp_mem_addr & ~3;
361        // sp_mem_addr &= ~0x3;
362        // fatalerror("sp_dma (%s): sp_mem_addr unaligned: %08X\n", cpu ? "RSP" : "R4300i", sp_mem_addr);
363427   }
364428   if (sp_dram_addr & 0x7)
365429   {
366430        sp_dram_addr = sp_dram_addr & ~7;
367431   }
368432
369   if (sp_dma_count > 0)
370   {
371      // fatalerror("sp_dma: dma_count = %d\n", sp_dma_count);
372   }
373   if (sp_dma_skip > 0)
374   {
375      // fatalerror("sp_dma: dma_skip = %d\n", sp_dma_skip);
376   }
377
378433   if ((sp_mem_addr & 0xfff) + (sp_dma_length) > 0x1000)
379434   {
380435      printf("sp_dma: dma out of memory area: %08X, %08X\n", sp_mem_addr, sp_dma_length);
r14029r14030
382437      sp_dma_length = 0x1000 - (sp_mem_addr & 0xfff);
383438   }
384439
385   if (direction == 0)      // RDRAM -> I/DMEM
440   if(direction == 0)// RDRAM -> I/DMEM
386441   {
387        for (c=0; c <= sp_dma_count; c++)
442        for(int c = 0; c <= sp_dma_count; c++)
388443        {
389444            UINT32 src = sp_dram_addr;
390445            UINT32 dst = 0x04000000 | (sp_mem_addr & 0x1fff);
391446
392447         //printf("CPU %08x -> RSP %08x\n", sp_dram_addr, 0x04000000 | sp_mem_addr);
393            for (i=0; i < sp_dma_length; i++)
448            for(int i = 0; i < sp_dma_length; i++)
394449            {
395            //printf("%02x ", space->read_byte((src + i)^3));
396            space->write_byte(dst + i, space->read_byte(src + i));
450            //printf("%02x ", mem_map->read_byte((src + i)^3));
451            mem_map->write_byte(dst + i, mem_map->read_byte(src + i));
397452            }
398453            //printf("\n");
399454
r14029r14030
405460   }
406461   else               // I/DMEM -> RDRAM
407462   {
408        for (c=0; c <= sp_dma_count; c++)
463        for(int c = 0; c <= sp_dma_count; c++)
409464        {
410465            UINT32 src = 0x04000000 | (sp_mem_addr & 0x1fff);
411466            UINT32 dst = sp_dram_addr;
412467
413468         //printf("RSP %08x -> CPU %08x\n", 0x04000000 | sp_mem_addr, sp_dram_addr);
414            for (i=0; i < sp_dma_length; i++)
469            for(int i = 0; i < sp_dma_length; i++)
415470            {
416            //printf("%02x ", space->read_byte((src + i)^3));
417            space->write_byte(dst + i, space->read_byte(src + i));
471            //printf("%02x ", mem_map->read_byte((src + i)^3));
472            mem_map->write_byte(dst + i, mem_map->read_byte(src + i));
418473            }
419474            //printf("\n");
420475
r14029r14030
428483
429484static void sp_set_status(device_t *device, UINT32 status)
430485{
486   device->machine().device<n64_periphs>("rcp")->sp_set_status(status);
487}
488
489void n64_periphs::sp_set_status(UINT32 status)
490{
431491   if (status & 0x1)
432492   {
433      //device->machine().scheduler().trigger(6789);
434
435      device_set_input_line(device, INPUT_LINE_HALT, ASSERT_LINE);
436        cpu_set_reg(device, RSP_SR, cpu_get_reg(device, RSP_SR) | RSP_STATUS_HALT);
437      //rsp_sp_status |= SP_STATUS_HALT;
493      device_set_input_line(rspcpu, INPUT_LINE_HALT, ASSERT_LINE);
494        cpu_set_reg(rspcpu, RSP_SR, cpu_get_reg(rspcpu, RSP_SR) | RSP_STATUS_HALT);
438495   }
496
439497   if (status & 0x2)
440498   {
441      //rsp_sp_status |= SP_STATUS_BROKE;
442        cpu_set_reg(device, RSP_SR, cpu_get_reg(device, RSP_SR) | RSP_STATUS_BROKE);
499        cpu_set_reg(rspcpu, RSP_SR, cpu_get_reg(rspcpu, RSP_SR) | RSP_STATUS_BROKE);
443500
444        if (cpu_get_reg(device, RSP_SR) & RSP_STATUS_INTR_BREAK)
501        if (cpu_get_reg(rspcpu, RSP_SR) & RSP_STATUS_INTR_BREAK)
445502      {
446         signal_rcp_interrupt(device->machine(), SP_INTERRUPT);
503         signal_rcp_interrupt(SP_INTERRUPT);
447504      }
448505   }
449506}
450507
451READ32_DEVICE_HANDLER( n64_sp_reg_r )
508UINT32 n64_periphs::sp_reg_r(UINT32 offset)
452509{
510   //printf("sp_reg_r %08x\n", offset * 4);
453511   switch (offset)
454512   {
455513      case 0x00/4:      // SP_MEM_ADDR_REG
r14029r14030
462520         return (sp_dma_skip << 20) | (sp_dma_count << 12) | sp_dma_length;
463521
464522      case 0x10/4:      // SP_STATUS_REG
465            return cpu_get_reg(device, RSP_SR);
523            return cpu_get_reg(rspcpu, RSP_SR);
466524
467525      case 0x14/4:      // SP_DMA_FULL_REG
468526         return 0;
r14029r14030
496554           return ++dp_clock;
497555
498556        case 0x40000/4:     // PC
499            return cpu_get_reg(device, RSP_PC) & 0x00000fff;
557            return cpu_get_reg(rspcpu, RSP_PC) & 0x00000fff;
500558
501559        default:
502            logerror("sp_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(device));
560            logerror("sp_reg_r: %08X at %08X\n", offset, cpu_get_pc(maincpu));
503561            break;
504562   }
505563
506564   return 0;
507565}
508566
509WRITE32_DEVICE_HANDLER( n64_sp_reg_w )
567READ32_DEVICE_HANDLER( n64_sp_reg_r )
510568{
569   return device->machine().device<n64_periphs>("rcp")->sp_reg_r(offset);
570}
571
572void n64_periphs::sp_reg_w(UINT32 offset, UINT32 data, UINT32 mem_mask)
573{
574   //printf("sp_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
511575   if ((offset & 0x10000) == 0)
512576   {
513577      switch (offset & 0xffff)
r14029r14030
524588            sp_dma_length = data & 0xfff;
525589            sp_dma_count = (data >> 12) & 0xff;
526590            sp_dma_skip = (data >> 20) & 0xfff;
527            sp_dma(device->machine(), 0);
591            sp_dma(0);
528592            break;
529593
530594         case 0x0c/4:      // SP_WR_LEN_REG
531595            sp_dma_length = data & 0xfff;
532596            sp_dma_count = (data >> 12) & 0xff;
533597            sp_dma_skip = (data >> 20) & 0xfff;
534            sp_dma(device->machine(), 1);
598            sp_dma(1);
535599            break;
536600
537601            case 0x10/4:        // RSP_STATUS_REG
538602            {
539               UINT32 oldstatus = cpu_get_reg(device, RSP_SR);
603               UINT32 oldstatus = cpu_get_reg(rspcpu, RSP_SR);
540604               UINT32 newstatus = oldstatus;
541605
542606                // printf( "RSP_STATUS_REG Write; %08x\n", data );
543607                if (data & 0x00000001)      // clear halt
544608                {
545                    //if (first_rsp)
546                    //{
547                    //  device_spin_until_trigger(device, 6789);
548
549                        // printf( "Clearing RSP_STATUS_HALT\n" );
550                        device_set_input_line(device, INPUT_LINE_HALT, CLEAR_LINE);
551                        newstatus &= ~RSP_STATUS_HALT;
552                        // RSP_STATUS &= ~RSP_STATUS_HALT;
553                    //}
554                    //else
555                    //{
556                    //  first_rsp = 1;
557                    //}
609               device_set_input_line(rspcpu, INPUT_LINE_HALT, CLEAR_LINE);
610               newstatus &= ~RSP_STATUS_HALT;
558611                }
559612                if (data & 0x00000002)      // set halt
560613                {
561                    // printf( "Setting RSP_STATUS_HALT\n" );
562                    device_set_input_line(device, INPUT_LINE_HALT, ASSERT_LINE);
614                    device_set_input_line(rspcpu, INPUT_LINE_HALT, ASSERT_LINE);
563615                    newstatus |= RSP_STATUS_HALT;
564                    // RSP_STATUS |= RSP_STATUS_HALT;
565616                }
566617                if (data & 0x00000004)
567618                {
568                    //printf( "Clearing RSP_STATUS_BROKE\n" );
569619                    newstatus &= ~RSP_STATUS_BROKE;
570                    // RSP_STATUS &= ~RSP_STATUS_BROKE;     // clear broke
571620                }
572621                if (data & 0x00000008)      // clear interrupt
573622                {
574                    clear_rcp_interrupt(device->machine(), SP_INTERRUPT);
623                    clear_rcp_interrupt(SP_INTERRUPT);
575624                }
576625                if (data & 0x00000010)      // set interrupt
577626                {
578                    signal_rcp_interrupt(device->machine(), SP_INTERRUPT);
627                    signal_rcp_interrupt(SP_INTERRUPT);
579628                }
580629                if (data & 0x00000020)
581630                {
582                    // printf( "Clearing RSP_STATUS_SSTEP\n" );
583631                    newstatus &= ~RSP_STATUS_SSTEP;
584                    // RSP_STATUS &= ~RSP_STATUS_SSTEP;     // clear single step
585632                }
586633                if (data & 0x00000040)
587634                {
588                    //printf( "Setting RSP_STATUS_SSTEP\n" );
589                    newstatus |= RSP_STATUS_SSTEP;
590                    if( !( oldstatus & ( RSP_STATUS_BROKE | RSP_STATUS_HALT ) ) )
635                    newstatus |= RSP_STATUS_SSTEP;   // set single step
636                    if(!(oldstatus & (RSP_STATUS_BROKE | RSP_STATUS_HALT)))
591637                    {
592                        cpu_set_reg(device, RSP_STEPCNT, 1 );
638                        cpu_set_reg(rspcpu, RSP_STEPCNT, 1 );
593639                    }
594                    // RSP_STATUS |= RSP_STATUS_SSTEP;      // set single step
595640                }
596641                if (data & 0x00000080)
597642                {
598                    newstatus &= ~RSP_STATUS_INTR_BREAK;
599                    // RSP_STATUS &= ~RSP_STATUS_INTR_BREAK;    // clear interrupt on break
643                    newstatus &= ~RSP_STATUS_INTR_BREAK;   // clear interrupt on break
600644                }
601645                if (data & 0x00000100)
602646                {
603                    newstatus |= RSP_STATUS_INTR_BREAK;
604                    // RSP_STATUS |= RSP_STATUS_INTR_BREAK; // set interrupt on break
647                    newstatus |= RSP_STATUS_INTR_BREAK;      // set interrupt on break
605648                }
606649                if (data & 0x00000200)
607650                {
608                    newstatus &= ~RSP_STATUS_SIGNAL0;
609                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL0;       // clear signal 0
651                    newstatus &= ~RSP_STATUS_SIGNAL0;      // clear signal 0
610652                }
611653                if (data & 0x00000400)
612654                {
613                    newstatus |= RSP_STATUS_SIGNAL0;
614                    // RSP_STATUS |= RSP_STATUS_SIGNAL0;        // set signal 0
655                    newstatus |= RSP_STATUS_SIGNAL0;      // set signal 0
615656                }
616657                if (data & 0x00000800)
617658                {
618                    newstatus &= ~RSP_STATUS_SIGNAL1;
619                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL1;       // clear signal 1
659                    newstatus &= ~RSP_STATUS_SIGNAL1;      // clear signal 1
620660                }
621661                if (data & 0x00001000)
622662                {
623                    newstatus |= RSP_STATUS_SIGNAL1;
624                    // RSP_STATUS |= RSP_STATUS_SIGNAL1;        // set signal 1
663                    newstatus |= RSP_STATUS_SIGNAL1;      // set signal 1
625664                }
626665                if (data & 0x00002000)
627666                {
628                    newstatus &= ~RSP_STATUS_SIGNAL2 ;
629                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL2;       // clear signal 2
667                    newstatus &= ~RSP_STATUS_SIGNAL2 ;      // clear signal 2
630668                }
631669                if (data & 0x00004000)
632670                {
633                    newstatus |= RSP_STATUS_SIGNAL2;
634                    // RSP_STATUS |= RSP_STATUS_SIGNAL2;        // set signal 2
671                    newstatus |= RSP_STATUS_SIGNAL2;      // set signal 2
635672                }
636673                if (data & 0x00008000)
637674                {
638                    newstatus &= ~RSP_STATUS_SIGNAL3;
639                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL3;       // clear signal 3
675                    newstatus &= ~RSP_STATUS_SIGNAL3;      // clear signal 3
640676                }
641677                if (data & 0x00010000)
642678                {
643                    newstatus |= RSP_STATUS_SIGNAL3;
644                    // RSP_STATUS |= RSP_STATUS_SIGNAL3;        // set signal 3
679                    newstatus |= RSP_STATUS_SIGNAL3;      // set signal 3
645680                }
646681                if (data & 0x00020000)
647682                {
648                    newstatus &= ~RSP_STATUS_SIGNAL4;
649                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL4;       // clear signal 4
683                    newstatus &= ~RSP_STATUS_SIGNAL4;      // clear signal 4
650684                }
651685                if (data & 0x00040000)
652686                {
653                    newstatus |= RSP_STATUS_SIGNAL4;
654                    // RSP_STATUS |= RSP_STATUS_SIGNAL4;        // set signal 4
687                    newstatus |= RSP_STATUS_SIGNAL4;      // set signal 4
655688                }
656689                if (data & 0x00080000)
657690                {
658                    newstatus &= ~RSP_STATUS_SIGNAL5;
659                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL5;       // clear signal 5
691                    newstatus &= ~RSP_STATUS_SIGNAL5;      // clear signal 5
660692                }
661693                if (data & 0x00100000)
662694                {
663                    newstatus |= RSP_STATUS_SIGNAL5;
664                    // RSP_STATUS |= RSP_STATUS_SIGNAL5;        // set signal 5
695                    newstatus |= RSP_STATUS_SIGNAL5;      // set signal 5
665696                }
666697                if (data & 0x00200000)
667698                {
668                    newstatus &= ~RSP_STATUS_SIGNAL6;
669                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL6;       // clear signal 6
699                    newstatus &= ~RSP_STATUS_SIGNAL6;      // clear signal 6
670700                }
671701                if (data & 0x00400000)
672702                {
673                    newstatus |= RSP_STATUS_SIGNAL6;
674                    // RSP_STATUS |= RSP_STATUS_SIGNAL6;        // set signal 6
703                    newstatus |= RSP_STATUS_SIGNAL6;      // set signal 6
675704                }
676705                if (data & 0x00800000)
677706                {
678                    newstatus &= ~RSP_STATUS_SIGNAL7;
679                    // RSP_STATUS &= ~RSP_STATUS_SIGNAL7;       // clear signal 7
707                    newstatus &= ~RSP_STATUS_SIGNAL7;      // clear signal 7
680708                }
681709                if (data & 0x01000000)
682710                {
683                    newstatus |= RSP_STATUS_SIGNAL7;
684                    // RSP_STATUS |= RSP_STATUS_SIGNAL7;        // set signal 7
711                    newstatus |= RSP_STATUS_SIGNAL7;      // set signal 7
685712                }
686                cpu_set_reg(device, RSP_SR, newstatus);
713                cpu_set_reg(rspcpu, RSP_SR, newstatus);
687714                break;
688715            }
689716
r14029r14030
692719            {
693720                   sp_semaphore = 0;
694721            }
695      //      mame_printf_debug("sp_semaphore = %08X\n", sp_semaphore);
696722            break;
697723
698724         default:
699            logerror("sp_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(device));
725            logerror("sp_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
700726            break;
701727      }
702728   }
r14029r14030
705731        switch (offset & 0xffff)
706732        {
707733            case 0x00/4:        // SP_PC_REG
708                //printf( "Setting PC to: %08x\n", 0x04001000 | (data & 0xfff ) );
709                if( cpu_get_reg(device, RSP_NEXTPC) != 0xffffffff )
734                if( cpu_get_reg(rspcpu, RSP_NEXTPC) != 0xffffffff )
710735                {
711                    cpu_set_reg(device, RSP_NEXTPC, 0x1000 | (data & 0xfff));
736                    cpu_set_reg(rspcpu, RSP_NEXTPC, 0x1000 | (data & 0xfff));
712737                }
713738                else
714739                {
715                    cpu_set_reg(device, RSP_PC, 0x1000 | (data & 0xfff));
740                    cpu_set_reg(rspcpu, RSP_PC, 0x1000 | (data & 0xfff));
716741                }
717742                break;
718743
719744            default:
720                logerror("sp_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(device));
745                logerror("sp_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
721746                break;
722747      }
723748   }
724749}
725750
751WRITE32_DEVICE_HANDLER( n64_sp_reg_w )
752{
753   device->machine().device<n64_periphs>("rcp")->sp_reg_w(offset, data, mem_mask);
754}
755
726756// RDP Interface
727757
728758void dp_full_sync(running_machine &machine)
r14029r14030
807837
808838
809839// Video Interface
810UINT32 n64_vi_width;
811UINT32 n64_vi_origin;
812UINT32 n64_vi_control;
813UINT32 n64_vi_blank;
814UINT32 n64_vi_hstart;
815UINT32 n64_vi_vstart;
816UINT32 n64_vi_xscale;
817UINT32 n64_vi_yscale;
818static UINT32 n64_vi_burst, n64_vi_vsync,  n64_vi_hsync,  n64_vi_leap;
819static UINT32 n64_vi_intr,  n64_vi_vburst;
820
821
822static void n64_vi_recalculate_resolution(running_machine &machine)
840void n64_periphs::vi_recalculate_resolution()
823841{
824   _n64_state *state = machine.driver_data<_n64_state>();
842   _n64_state *state = machine().driver_data<_n64_state>();
825843
826    int x_start = (n64_vi_hstart & 0x03ff0000) >> 16;
827    int x_end = n64_vi_hstart & 0x000003ff;
828    int y_start = ((n64_vi_vstart & 0x03ff0000) >> 16) / 2;
829    int y_end = (n64_vi_vstart & 0x000003ff) / 2;
830    int width = ((n64_vi_xscale & 0x00000fff) * (x_end - x_start)) / 0x400;
831    int height = ((n64_vi_yscale & 0x00000fff) * (y_end - y_start)) / 0x400;
832    rectangle visarea = machine.primary_screen->visible_area();
833    attoseconds_t period = machine.primary_screen->frame_period().attoseconds;
844    int x_start = (vi_hstart & 0x03ff0000) >> 16;
845    int x_end = vi_hstart & 0x000003ff;
846    int y_start = ((vi_vstart & 0x03ff0000) >> 16) / 2;
847    int y_end = (vi_vstart & 0x000003ff) / 2;
848    int width = ((vi_xscale & 0x00000fff) * (x_end - x_start)) / 0x400;
849    int height = ((vi_yscale & 0x00000fff) * (y_end - y_start)) / 0x400;
850    rectangle visarea = machine().primary_screen->visible_area();
851    attoseconds_t period = machine().primary_screen->frame_period().attoseconds;
834852
835853    if (width == 0 || height == 0)
836854    {
837        n64_vi_blank = 1;
855        vi_blank = 1;
838856        /*
839857        FIXME: MAME doesn't handle well a h/w res of zero (otherwise it hardlocks the emu, seen especially in Aleck 64 games
840858        that sets the res after a longer delay than n64), guess that this just disables drawing?
r14029r14030
843861    }
844862    else
845863    {
846        n64_vi_blank = 0;
864        vi_blank = 0;
847865    }
848866
849//  if (width == 0)
850//      width = 1;
851
852//  if (height == 0)
853//      height = 1;
854
855867    if (width > 640)
856868        width = 640;
857869
r14029r14030
862874
863875    visarea.max_x = width - 1;
864876    visarea.max_y = height - 1;
865    machine.primary_screen->configure(width, 525, visarea, period);
877    machine().primary_screen->configure(width, 525, visarea, period);
866878}
867879
868READ32_HANDLER( n64_vi_reg_r )
880READ32_MEMBER( n64_periphs::vi_reg_r )
869881{
882   //printf("vi_reg_r %08x\n", offset * 4);
870883   switch (offset)
871884   {
872885      case 0x00/4:      // VI_CONTROL_REG
873         return n64_vi_control;
886         return vi_control;
874887
875888      case 0x04/4:      // VI_ORIGIN_REG
876            return n64_vi_origin;
889            return vi_origin;
877890
878891      case 0x08/4:      // VI_WIDTH_REG
879            return n64_vi_width;
892            return vi_width;
880893
881894      case 0x0c/4:
882            return n64_vi_intr;
895            return vi_intr;
883896
884897      case 0x10/4:      // VI_CURRENT_REG
885         return space->machine().primary_screen->vpos();
898         return machine().primary_screen->vpos();
886899
887900      case 0x14/4:      // VI_BURST_REG
888            return n64_vi_burst;
901            return vi_burst;
889902
890903      case 0x18/4:      // VI_V_SYNC_REG
891            return n64_vi_vsync;
904            return vi_vsync;
892905
893906      case 0x1c/4:      // VI_H_SYNC_REG
894            return n64_vi_hsync;
907            return vi_hsync;
895908
896909      case 0x20/4:      // VI_LEAP_REG
897            return n64_vi_leap;
910            return vi_leap;
898911
899912      case 0x24/4:      // VI_H_START_REG
900            return n64_vi_hstart;
913            return vi_hstart;
901914
902915      case 0x28/4:      // VI_V_START_REG
903            return n64_vi_vstart;
916            return vi_vstart;
904917
905918      case 0x2c/4:      // VI_V_BURST_REG
906            return n64_vi_vburst;
919            return vi_vburst;
907920
908921      case 0x30/4:      // VI_X_SCALE_REG
909            return n64_vi_xscale;
922            return vi_xscale;
910923
911924      case 0x34/4:      // VI_Y_SCALE_REG
912            return n64_vi_yscale;
925            return vi_yscale;
913926
914927      default:
915         logerror("vi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
928         logerror("vi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(maincpu));
916929         break;
917930   }
918931   return 0;
919932}
920933
921WRITE32_HANDLER( n64_vi_reg_w )
934WRITE32_MEMBER( n64_periphs::vi_reg_w )
922935{
923   _n64_state *state = space->machine().driver_data<_n64_state>();
936   //printf("vi_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
937   _n64_state *state = machine().driver_data<_n64_state>();
924938
925939   switch (offset)
926940   {
927941      case 0x00/4:      // VI_CONTROL_REG
928            n64_vi_control = data;
929            n64_vi_recalculate_resolution(space->machine());
942            vi_control = data;
943            vi_recalculate_resolution();
930944         break;
931945
932946      case 0x04/4:      // VI_ORIGIN_REG
933            n64_vi_origin = data & 0xffffff;
947            vi_origin = data & 0xffffff;
934948         break;
935949
936950      case 0x08/4:      // VI_WIDTH_REG
937            if (n64_vi_width != data && data > 0)
951            if (vi_width != data && data > 0)
938952         {
939                n64_vi_recalculate_resolution(space->machine());
953                vi_recalculate_resolution();
940954         }
941            n64_vi_width = data;
955            vi_width = data;
942956          state->m_rdp.GetMiscState()->m_fb_width = data;
943957         break;
944958
945959      case 0x0c/4:      // VI_INTR_REG
946            n64_vi_intr = data;
960            vi_intr = data;
947961         break;
948962
949963      case 0x10/4:      // VI_CURRENT_REG
950         clear_rcp_interrupt(space->machine(), VI_INTERRUPT);
964         clear_rcp_interrupt(VI_INTERRUPT);
951965         break;
952966
953967      case 0x14/4:      // VI_BURST_REG
954            n64_vi_burst = data;
968            vi_burst = data;
955969         break;
956970
957971      case 0x18/4:      // VI_V_SYNC_REG
958            n64_vi_vsync = data;
972            vi_vsync = data;
959973         break;
960974
961975      case 0x1c/4:      // VI_H_SYNC_REG
962            n64_vi_hsync = data;
976            vi_hsync = data;
963977         break;
964978
965979      case 0x20/4:      // VI_LEAP_REG
966            n64_vi_leap = data;
980            vi_leap = data;
967981         break;
968982
969983      case 0x24/4:      // VI_H_START_REG
970            n64_vi_hstart = data;
971            n64_vi_recalculate_resolution(space->machine());
984            vi_hstart = data;
985            vi_recalculate_resolution();
972986         break;
973987
974988      case 0x28/4:      // VI_V_START_REG
975            n64_vi_vstart = data;
976            n64_vi_recalculate_resolution(space->machine());
989            vi_vstart = data;
990            vi_recalculate_resolution();
977991         break;
978992
979993      case 0x2c/4:      // VI_V_BURST_REG
980            n64_vi_vburst = data;
994            vi_vburst = data;
981995         break;
982996
983997      case 0x30/4:      // VI_X_SCALE_REG
984            n64_vi_xscale = data;
985            n64_vi_recalculate_resolution(space->machine());
998            vi_xscale = data;
999            vi_recalculate_resolution();
9861000         break;
9871001
9881002      case 0x34/4:      // VI_Y_SCALE_REG
989            n64_vi_yscale = data;
990            n64_vi_recalculate_resolution(space->machine());
1003            vi_yscale = data;
1004            vi_recalculate_resolution();
9911005         break;
9921006
9931007        /*
9941008        Uncomment this for convenient homebrew debugging
995        */
9961009        case 0x44/4:        // TEMP DEBUG
9971010            printf( "E Ping: %08x\n", data );
9981011            break;
1012        */
9991013
10001014      default:
1001         logerror("vi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
1015         logerror("vi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
10021016         break;
10031017   }
10041018}
10051019
10061020
10071021// Audio Interface
1008static UINT32 ai_dram_addr;
1009static UINT32 ai_len;
1010static UINT32 ai_control = 0;
1011static int ai_dacrate;
1012static int ai_bitrate;
1013static UINT32 ai_status = 0;
1014
1015static emu_timer *audio_timer;
1016
1017#define AUDIO_DMA_DEPTH     2
1018
1019static void start_audio_dma(running_machine &machine);
1020
1021typedef struct
1022void n64_periphs::ai_fifo_push(UINT32 address, UINT32 length)
10221023{
1023    UINT32 address;
1024    UINT32 length;
1025} AUDIO_DMA;
1026
1027static AUDIO_DMA audio_fifo[AUDIO_DMA_DEPTH];
1028static int audio_fifo_wpos = 0;
1029static int audio_fifo_rpos = 0;
1030static int audio_fifo_num = 0;
1031
1032static void audio_fifo_push(running_machine &machine, UINT32 address, UINT32 length)
1033{
1034//    AUDIO_DMA *current;
1035
1036    if (audio_fifo_num == AUDIO_DMA_DEPTH)
1024    if (ai_fifo_num == AUDIO_DMA_DEPTH)
10371025    {
1038        mame_printf_debug("audio_fifo_push: tried to push to full DMA FIFO!!!\n");
1026        mame_printf_debug("ai_fifo_push: tried to push to full DMA FIFO!!!\n");
10391027    }
10401028
1041//  mame_printf_debug("fifo_push: adr %08x len %08x\n", address, length);
1029    ai_fifo[ai_fifo_wpos].address = address;
1030    ai_fifo[ai_fifo_wpos].length = length;
10421031
1043    audio_fifo[audio_fifo_wpos].address = address;
1044    audio_fifo[audio_fifo_wpos].length = length;
1045//    current = &audio_fifo[audio_fifo_wpos];
1032    ai_fifo_wpos++;
1033    ai_fifo_num++;
10461034
1047    audio_fifo_wpos++;
1048    audio_fifo_num++;
1049
1050    if (audio_fifo_wpos >= AUDIO_DMA_DEPTH)
1035    if (ai_fifo_wpos >= AUDIO_DMA_DEPTH)
10511036    {
1052        audio_fifo_wpos = 0;
1037        ai_fifo_wpos = 0;
10531038    }
10541039
1055    if (audio_fifo_num >= AUDIO_DMA_DEPTH)
1040    if (ai_fifo_num >= AUDIO_DMA_DEPTH)
10561041    {
10571042        ai_status |= 0x80000001;    // FIFO full
10581043    }
10591044
10601045    if (! (ai_status & 0x40000000))
10611046    {
1062        signal_rcp_interrupt(machine, AI_INTERRUPT);
1063        start_audio_dma(machine);
1047        signal_rcp_interrupt(AI_INTERRUPT);
1048        ai_dma();
10641049    }
10651050}
10661051
1067static void audio_fifo_pop(running_machine &machine)
1052void n64_periphs::ai_fifo_pop()
10681053{
1069    audio_fifo_rpos++;
1070    audio_fifo_num--;
1054    ai_fifo_rpos++;
1055    ai_fifo_num--;
10711056
1072    if (audio_fifo_num < 0)
1057    if (ai_fifo_num < 0)
10731058    {
1074        fatalerror("audio_fifo_pop: FIFO underflow!\n");
1059        fatalerror("ai_fifo_pop: FIFO underflow!\n");
10751060    }
10761061
1077    if (audio_fifo_rpos >= AUDIO_DMA_DEPTH)
1062    if (ai_fifo_rpos >= AUDIO_DMA_DEPTH)
10781063    {
1079        audio_fifo_rpos = 0;
1064        ai_fifo_rpos = 0;
10801065    }
10811066
1082    if (audio_fifo_num < AUDIO_DMA_DEPTH)
1067    if (ai_fifo_num < AUDIO_DMA_DEPTH)
10831068    {
10841069        ai_status &= ~0x80000001;   // FIFO not full
1085        signal_rcp_interrupt(machine, AI_INTERRUPT);
1070        signal_rcp_interrupt(AI_INTERRUPT);
10861071    }
10871072}
10881073
1089static AUDIO_DMA *audio_fifo_get_top(void)
1074n64_periphs::AUDIO_DMA *n64_periphs::ai_fifo_get_top()
10901075{
1091    if (audio_fifo_num > 0)
1076    if (ai_fifo_num > 0)
10921077    {
1093        return &audio_fifo[audio_fifo_rpos];
1078        return &ai_fifo[ai_fifo_rpos];
10941079    }
10951080    else
10961081    {
r14029r14030
11001085
11011086#define N64_ATTOTIME_NORMALIZE(a)   do { while ((a).attoseconds >= ATTOSECONDS_PER_SECOND) { (a).seconds++; (a).attoseconds -= ATTOSECONDS_PER_SECOND; } } while (0)
11021087
1103static void start_audio_dma(running_machine &machine)
1088void n64_periphs::ai_dma()
11041089{
11051090    INT16 *ram = (INT16*)rdram;
1106    AUDIO_DMA *current = audio_fifo_get_top();
1091    AUDIO_DMA *current = ai_fifo_get_top();
11071092    attotime period;
11081093
11091094    //static FILE * audio_dump = NULL;
r14029r14030
11171102
11181103//  mame_printf_debug("DACDMA: %x for %x bytes\n", current->address, current->length);
11191104
1120   dmadac[0] = machine.device<dmadac_sound_device>("dac1");
1121   dmadac[1] = machine.device<dmadac_sound_device>("dac2");
1122    dmadac_transfer(&dmadac[0], 2, 1, 2, current->length/4, ram);
1105    dmadac_transfer(&ai_dac[0], 2, 1, 2, current->length/4, ram);
11231106
11241107    ai_status |= 0x40000000;
11251108
11261109   // adjust the timer
11271110   period = attotime::from_hz(DACRATE_NTSC) * ((ai_dacrate + 1) * (current->length / 4));
1128   audio_timer->adjust(period);
1111   ai_timer->adjust(period);
11291112}
11301113
1131static TIMER_CALLBACK( audio_timer_callback )
1114static TIMER_CALLBACK(ai_timer_callback)
11321115{
1133    audio_fifo_pop(machine);
1116   machine.device<n64_periphs>("rcp")->ai_timer_tick();
1117}
11341118
1119void n64_periphs::ai_timer_tick()
1120{
1121    ai_fifo_pop();
1122
11351123    // keep playing if there's another DMA queued
1136    if (audio_fifo_get_top() != NULL)
1124    if (ai_fifo_get_top() != NULL)
11371125    {
1138        start_audio_dma(machine);
1139        signal_rcp_interrupt(machine, AI_INTERRUPT);
1126        ai_dma();
1127        signal_rcp_interrupt(AI_INTERRUPT);
11401128    }
11411129    else
11421130    {
r14029r14030
11441132    }
11451133}
11461134
1147READ32_HANDLER( n64_ai_reg_r )
1135READ32_MEMBER( n64_periphs::ai_reg_r )
11481136{
1137   //printf("ai_reg_r %08x\n", offset * 4);
11491138    switch (offset)
11501139    {
11511140        case 0x04/4:        // AI_LEN_REG
r14029r14030
11561145            }
11571146            else if (ai_status & 0x40000000)
11581147            {
1159                double secs_left = (audio_timer->expire() - space->machine().time()).as_double();
1148                double secs_left = (ai_timer->expire() - machine().time()).as_double();
11601149                unsigned int samples_left = secs_left * DACRATE_NTSC / (ai_dacrate + 1);
11611150                return samples_left * 4;
11621151            }
r14029r14030
11671156            return ai_status;
11681157
11691158        default:
1170            logerror("ai_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
1159            logerror("ai_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(maincpu));
11711160            break;
11721161    }
11731162
11741163    return 0;
11751164}
11761165
1177WRITE32_HANDLER( n64_ai_reg_w )
1166WRITE32_MEMBER( n64_periphs::ai_reg_w )
11781167{
1179//  UINT16 *ram = (UINT16*)rdram;
1180
1168   //printf("ai_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
11811169    switch (offset)
11821170    {
11831171        case 0x00/4:        // AI_DRAM_ADDR_REG
1184//          mame_printf_debug("ai_dram_addr = %08X at %08X\n", data, cpu_get_pc(&space->device()));
11851172            ai_dram_addr = data & 0xffffff;
11861173            break;
11871174
11881175        case 0x04/4:        // AI_LEN_REG
1189//          mame_printf_debug("ai_len = %08X at %08X\n", data, cpu_get_pc(&space->device()));
11901176            ai_len = data & 0x3ffff;        // Hardware v2.0 has 18 bits, v1.0 has 15 bits
1191            audio_fifo_push(space->machine(), ai_dram_addr, ai_len);
1177            ai_fifo_push(ai_dram_addr, ai_len);
11921178            break;
11931179
11941180        case 0x08/4:        // AI_CONTROL_REG
1195//          mame_printf_debug("ai_control = %08X at %08X\n", data, cpu_get_pc(&space->device()));
11961181            ai_control = data;
11971182            break;
11981183
11991184        case 0x0c/4:
1200            clear_rcp_interrupt(space->machine(), AI_INTERRUPT);
1185            clear_rcp_interrupt(AI_INTERRUPT);
12011186            break;
12021187
12031188        case 0x10/4:        // AI_DACRATE_REG
12041189            ai_dacrate = data & 0x3fff;
1205            dmadac_set_frequency(&dmadac[0], 2, (double)DACRATE_NTSC / (double)(ai_dacrate+1));
1206            printf( "frequency: %f\n", (double)DACRATE_NTSC / (double)(ai_dacrate+1) );
1207            dmadac_enable(&dmadac[0], 2, 1);
1190            dmadac_set_frequency(&ai_dac[0], 2, (double)DACRATE_NTSC / (double)(ai_dacrate+1));
1191            //printf( "frequency: %f\n", (double)DACRATE_NTSC / (double)(ai_dacrate+1) );
1192            dmadac_enable(&ai_dac[0], 2, 1);
12081193            break;
12091194
12101195        case 0x14/4:        // AI_BITRATE_REG
1211//          mame_printf_debug("ai_bitrate = %08X\n", data);
12121196            ai_bitrate = data & 0xf;
12131197            break;
12141198
12151199        default:
1216            logerror("ai_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
1200            logerror("ai_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
12171201            break;
12181202    }
12191203}
r14029r14030
12211205
12221206// Peripheral Interface
12231207
1224static UINT32 pi_dram_addr, pi_cart_addr;
1225static UINT32 pi_first_dma = 1;
1226static UINT32 pi_rd_len = 0;
1227static UINT32 pi_wr_len = 0;
1228static UINT32 pi_status = 0;
1229static UINT32 pi_bsd_dom1_lat = 0;
1230static UINT32 pi_bsd_dom1_pwd = 0;
1231static UINT32 pi_bsd_dom1_pgs = 0;
1232static UINT32 pi_bsd_dom1_rls = 0;
1233static UINT32 pi_bsd_dom2_lat = 0;
1234static UINT32 pi_bsd_dom2_pwd = 0;
1235static UINT32 pi_bsd_dom2_pgs = 0;
1236static UINT32 pi_bsd_dom2_rls = 0;
1208static TIMER_CALLBACK(pi_dma_callback)
1209{
1210   machine.device<n64_periphs>("rcp")->pi_dma_tick();
1211}
12371212
1238READ32_HANDLER( n64_pi_reg_r )
1213void n64_periphs::pi_dma_tick()
12391214{
1215   //printf("pi_dma_tick\n");
1216   if(pi_dma_dir == 1)
1217   {
1218      UINT32 dma_length = pi_wr_len + 1;
1219      if (dma_length & 3)
1220      {
1221         dma_length = (dma_length + 3) & ~3;
1222      }
1223
1224      if (pi_dram_addr != 0xffffffff)
1225      {
1226         for(int i = 0; i < dma_length; i++)
1227         {
1228            UINT8 b = mem_map->read_byte(pi_cart_addr);
1229            mem_map->write_byte(pi_dram_addr & 0x1fffffff, b);
1230            pi_cart_addr += 1;
1231            pi_dram_addr += 1;
1232         }
1233      }
1234
1235      if (pi_first_dma)
1236      {
1237         // TODO: CIC-6105 has different address...
1238         mem_map->write_dword(0x00000318, 0x400000);
1239         mem_map->write_dword(0x000003f0, 0x800000);
1240         pi_first_dma = 0;
1241      }
1242   }
1243   else
1244   {
1245      UINT32 dma_length = pi_rd_len + 1;
1246      if (dma_length & 3)
1247      {
1248         dma_length = (dma_length + 3) & ~3;
1249      }
1250
1251      if (pi_dram_addr != 0xffffffff)
1252      {
1253         for(int i = 0; i < dma_length; i++)
1254         {
1255            UINT8 b = mem_map->read_byte(pi_dram_addr);
1256            mem_map->write_byte(pi_cart_addr & 0x1fffffff, b);
1257            pi_cart_addr += 1;
1258            pi_dram_addr += 1;
1259         }
1260      }
1261   }
1262
1263   pi_status &= ~1; // Clear DMA_BUSY
1264   pi_status |= 8; // Set INTERRUPT
1265
1266   signal_rcp_interrupt(PI_INTERRUPT);
1267
1268   pi_dma_timer->adjust(attotime::never);
1269}
1270
1271READ32_MEMBER( n64_periphs::pi_reg_r )
1272{
1273   //printf("pi_reg_r %08x\n", offset * 4);
12401274   switch (offset)
12411275   {
12421276      case 0x00/4:      // PI_DRAM_ADDR_REG
r14029r14030
12731307            return pi_bsd_dom2_rls;
12741308
12751309      default:
1276         logerror("pi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
1310         logerror("pi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(maincpu));
12771311         break;
12781312   }
12791313   return 0;
12801314}
12811315
1282WRITE32_HANDLER( n64_pi_reg_w )
1316WRITE32_MEMBER( n64_periphs::pi_reg_w )
12831317{
1318   //printf("pi_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
12841319   switch (offset)
12851320   {
12861321      case 0x00/4:      // PI_DRAM_ADDR_REG
r14029r14030
12971332
12981333      case 0x08/4:      // PI_RD_LEN_REG
12991334      {
1300         int i;
1301         UINT32 dma_length = (data + 1);
13021335            pi_rd_len = data;
1336         pi_dma_dir = 0;
1337         pi_status |= 1;
13031338
1304         /*if (dma_length & 3)
1305            {
1306                dma_length = (dma_length + 3) & ~3;
1307            }*/
1308
1309         //mame_printf_debug("PI DMA: %08X to %08X, length %08X\n", pi_dram_addr, pi_cart_addr, dma_length);
1310
1311         if (pi_dram_addr != 0xffffffff)
1312         {
1313            for (i=0; i < dma_length; i++)
1314            {
1315               UINT8 b = space->read_byte(pi_dram_addr);
1316               space->write_byte(pi_cart_addr & 0x1fffffff, b);
1317               pi_cart_addr += 1;
1318               pi_dram_addr += 1;
1319            }
1320         }
1321
1322         signal_rcp_interrupt(space->machine(), PI_INTERRUPT);
1339            attotime dma_period = attotime::from_hz(93750000) * (pi_rd_len + 1) * 3;
1340            //printf("want read dma in %d\n", (pi_rd_len + 1));
1341            pi_dma_timer->adjust(dma_period);
13231342         break;
13241343      }
13251344
13261345      case 0x0c/4:      // PI_WR_LEN_REG
13271346      {
1328         int i;
1329         UINT32 dma_length = (data + 1);
13301347            pi_wr_len = data;
1348         pi_dma_dir = 1;
1349         pi_status |= 1;
13311350
1332         if (dma_length & 3)
1333            {
1334                dma_length = (dma_length + 3) & ~3;
1335            }
1336
1337         //printf("PI DMA: %08X to %08X, length %08X\n", pi_cart_addr, pi_dram_addr, dma_length);
1338
1339         if (pi_dram_addr != 0xffffffff)
1340         {
1341            for (i=0; i < dma_length; i++)
1342            {
1343               /*UINT32 d = space->read_dword(pi_cart_addr);
1344                    space->write_dword(pi_dram_addr, d);
1345                    pi_cart_addr += 4;
1346                    pi_dram_addr += 4;*/
1347
1348               UINT8 b = space->read_byte(pi_cart_addr);
1349               space->write_byte(pi_dram_addr & 0x1fffffff, b);
1350               pi_cart_addr += 1;
1351               pi_dram_addr += 1;
1352            }
1353         }
1354         signal_rcp_interrupt(space->machine(), PI_INTERRUPT);
1355
1356         if (pi_first_dma)
1357         {
1358            // TODO: CIC-6105 has different address...
1359            space->write_dword(0x00000318, 0x400000);
1360            space->write_dword(0x000003f0, 0x800000);
1361            pi_first_dma = 0;
1362         }
1363
1351            attotime dma_period = attotime::from_hz(93750000) * (pi_wr_len + 1) * 3;
1352            //printf("want write dma in %d\n", (pi_wr_len + 1));
1353            pi_dma_timer->adjust(dma_period);
13641354         break;
13651355      }
13661356
r14029r14030
13681358      {
13691359         if (data & 0x2)
13701360         {
1371            clear_rcp_interrupt(space->machine(), PI_INTERRUPT);
1361            pi_status &= ~8; // Clear INTERRUPT
1362            clear_rcp_interrupt(PI_INTERRUPT);
13721363         }
13731364         break;
13741365      }
r14029r14030
14061397            break;
14071398
14081399      default:
1409         logerror("pi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
1400         logerror("pi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
14101401         break;
14111402   }
14121403}
14131404
14141405// RDRAM Interface
1415static UINT32 ri_mode = 0;
1416static UINT32 ri_config = 0;
1417static UINT32 ri_current_load = 0;
1418static UINT32 ri_select = 0;
1419static UINT32 ri_count = 0;
1420static UINT32 ri_latency = 0;
1421static UINT32 ri_rerror = 0;
1422static UINT32 ri_werror = 0;
14231406
1424READ32_HANDLER( n64_ri_reg_r )
1407READ32_MEMBER( n64_periphs::ri_reg_r )
14251408{
1426    //printf( "n64_ri_reg_r: 0x%02x/4 (%08x)\n", offset << 2, mem_mask );
1427   switch (offset)
1409   //printf("ri_reg_r %08x\n", offset * 4);
1410   if(offset > 0x1c/4)
14281411   {
1429        case 0x00/4: // RI_MODE_REG
1430            return ri_mode;
1431
1432        case 0x04/4: // RI_CONFIG_REG
1433            return ri_config;
1434
1435        case 0x08/4: // RI_CURRENT_LOAD_REG
1436            return ri_current_load;
1437
1438        case 0x0c/4: // RI_SELECT_REG
1439            return ri_select;
1440
1441        case 0x10/4: // RI_COUNT_REG
1442            return ri_count;
1443
1444        case 0x14/4: // RI_LATENCY_REG
1445            return ri_latency;
1446
1447        case 0x18/4: // RI_RERROR_REG
1448            return ri_rerror;
1449
1450        case 0x1c/4: // RI_WERROR_REG
1451            return ri_werror;
1452
1453      default:
1454         logerror("ri_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(&space->device()));
1455         break;
1412      logerror("ri_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(maincpu));
1413      return 0;
14561414   }
1457
1458   return 0;
1415   return ri_regs[offset];
14591416}
14601417
1461WRITE32_HANDLER( n64_ri_reg_w )
1418WRITE32_MEMBER( n64_periphs::ri_reg_w )
14621419{
1463    //printf( "n64_ri_reg_w: 0x%02x/4 = %08x (%08x)\n", offset << 2, data, mem_mask );
1464   switch (offset)
1420   //printf("ri_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
1421   if(offset > 0x1c/4)
14651422   {
1466        case 0x00/4: // RI_MODE_REG
1467            ri_mode = data;
1468            break;
1469
1470        case 0x04/4: // RI_CONFIG_REG
1471            ri_config = data;
1472            break;
1473
1474        case 0x08/4: // RI_CURRENT_LOAD_REG
1475            ri_current_load = data;
1476            break;
1477
1478        case 0x0c/4: // RI_SELECT_REG
1479            ri_select = data;
1480            break;
1481
1482        case 0x10/4: // RI_COUNT_REG
1483            ri_count = data;
1484            break;
1485
1486        case 0x14/4: // RI_LATENCY_REG
1487            ri_latency = data;
1488            break;
1489
1490        case 0x18/4: // RI_RERROR_REG
1491            ri_rerror = data;
1492            break;
1493
1494        case 0x1c/4: // RI_WERROR_REG
1495            ri_werror = data;
1496            break;
1497
1498      default:
1499         logerror("ri_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(&space->device()));
1500         break;
1423      logerror("ri_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(maincpu));
1424      return;
15011425   }
1426   COMBINE_DATA(&ri_regs[offset]);
15021427}
15031428
15041429// Serial Interface
1505static UINT8 pif_ram[0x40];
1506static UINT8 pif_cmd[0x40];
1507static UINT32 si_dram_addr = 0;
1508static UINT32 si_pif_addr = 0;
1509static UINT32 si_pif_addr_rd64b = 0;
1510static UINT32 si_pif_addr_wr64b = 0;
1511static UINT32 si_status = 0;
1512
1513static UINT8 eeprom[512];
1514static UINT8 mempack[0x8000];
1515
1516static UINT8 calc_mempack_crc(UINT8 *buffer, int length)
1430UINT8 n64_periphs::calc_mempack_crc(UINT8 *buffer, int length)
15171431{
1518   int i, j;
15191432   UINT32 crc = 0;
15201433   UINT32 temp2 = 0;
15211434
1522   for (i=0; i <= length; i++)
1435   for(int i = 0; i <= length; i++)
15231436   {
1524      for (j=7; j >= 0; j--)
1437      for(int j = 7; j >= 0; j--)
15251438      {
15261439         if ((crc & 0x80) != 0)
15271440         {
r14029r14030
15531466   return crc;
15541467}
15551468
1556static int pif_channel_handle_command(running_machine &machine, int channel, int slength, UINT8 *sdata, int rlength, UINT8 *rdata)
1469int n64_periphs::pif_channel_handle_command(int channel, int slength, UINT8 *sdata, int rlength, UINT8 *rdata)
15571470{
1558   int i;
15591471   UINT8 command = sdata[0];
15601472
15611473   switch (command)
15621474   {
15631475      case 0x00:      // Read status
15641476      {
1565         if (slength != 1 || rlength != 3)
1566         {
1567            // osd_die("handle_pif: read status (bytes to send %d, bytes to receive %d)\n", bytes_to_send, bytes_to_recv);
1568         }
1569
15701477         switch (channel)
15711478         {
15721479            case 0:
r14029r14030
15881495               rdata[0] = 0x00;
15891496               rdata[1] = 0x80;
15901497               rdata[2] = 0x00;
1591               //rdata[0] = 0xff;
1592               //rdata[1] = 0xff;
1593               //rdata[2] = 0xff;
15941498
15951499               return 1;
15961500            }
r14029r14030
16211525            case 0: //p1 inputs
16221526            case 1: //p2 inputs
16231527            {
1624                    buttons = input_port_read(machine, portnames[(channel*3) + 0]);
1625                    x = input_port_read(machine, portnames[(channel*3) + 1]) - 128;
1626                    y = input_port_read(machine, portnames[(channel*3) + 2]) - 128;
1528                    buttons = input_port_read(machine(), portnames[(channel*3) + 0]);
1529                    x = input_port_read(machine(), portnames[(channel*3) + 1]) - 128;
1530                    y = input_port_read(machine(), portnames[(channel*3) + 2]) - 128;
16271531
16281532               rdata[0] = (buttons >> 8) & 0xff;
16291533               rdata[1] = (buttons >> 0) & 0xff;
r14029r14030
16461550      {
16471551         UINT32 address;
16481552
1649         /*mame_printf_debug("Read from mempack, rlength = %d, slength = %d\n", rlength, slength);
1650            for (i=0; i < slength; i++)
1651            {
1652                mame_printf_debug("%02X ", sdata[i]);
1653            }
1654            mame_printf_debug("\n");*/
1655
16561553         address = (sdata[1] << 8) | (sdata[2]);
1657//          checksum = address & 0x1f;
16581554         address &= ~0x1f;
16591555
1660         if (address == 0x400)
1556         if(address == 0x400)
16611557         {
1662            for (i=0; i < rlength-1; i++)
1558            for(int i = 0; i < rlength-1; i++)
16631559            {
16641560               rdata[i] = 0x00;
16651561            }
16661562
16671563            rdata[rlength-1] = calc_mempack_crc(rdata, rlength-1);
1668      //      mame_printf_debug("CRC = %02X\n", rdata[rlength-1]);
16691564         }
1670         else if (address < 0x7fe0)
1565         else if(address < 0x7fe0)
16711566         {
1672            for (i=0; i < rlength-1; i++)
1567            for(int i = 0; i < rlength-1; i++)
16731568            {
16741569               rdata[i] = mempack[address+i];
16751570            }
16761571
16771572            rdata[rlength-1] = calc_mempack_crc(rdata, rlength-1);
1678      //      mame_printf_debug("CRC = %02X\n", rdata[rlength-1]);
16791573         }
16801574         return 1;
16811575      }
16821576      case 0x03:
16831577      {
1684         UINT32 address;
1685         /*mame_printf_debug("Write to mempack, rlength = %d, slength = %d\n", rlength, slength);
1686            for (i=0; i < slength; i++)
1687            {
1688                mame_printf_debug("%02X ", sdata[i]);
1689            }
1690            mame_printf_debug("\n");*/
1691
1692         address = (sdata[1] << 8) | (sdata[2]);
1693//          checksum = address & 0x1f;
1578         UINT32 address = (sdata[1] << 8) | (sdata[2]);
16941579         address &= ~0x1f;
16951580
16961581         if (address == 0x8000)
r14029r14030
16991584         }
17001585         else
17011586         {
1702            for (i=3; i < slength; i++)
1587            for(int i = 3; i < slength; i++)
17031588            {
17041589               mempack[address++] = sdata[i];
17051590            }
r14029r14030
17121597
17131598      case 0x04:      // Read from EEPROM
17141599      {
1715         UINT8 block_offset;
1716
17171600         if (channel != 4)
17181601         {
1719            //fatalerror("Tried to write to EEPROM on channel %d\n", channel);
17201602            return 1;
17211603         }
17221604
r14029r14030
17251607            fatalerror("handle_pif: write EEPROM (bytes to send %d, bytes to receive %d)\n", slength, rlength);
17261608         }
17271609
1728         block_offset = sdata[1] * 8;
1610         UINT8 block_offset = sdata[1] * 8;
17291611
1730         for (i=0; i < 8; i++)
1612         for(int i=0; i < 8; i++)
17311613         {
17321614            rdata[i] = eeprom[block_offset+i];
17331615         }
r14029r14030
17371619
17381620      case 0x05:      // Write to EEPROM
17391621      {
1740         UINT8 block_offset;
1741
17421622         if (channel != 4)
17431623         {
1744            //fatalerror("Tried to write to EEPROM on channel %d\n", channel);
17451624            return 1;
17461625         }
17471626
r14029r14030
17501629            fatalerror("handle_pif: write EEPROM (bytes to send %d, bytes to receive %d)\n", slength, rlength);
17511630         }
17521631
1753         block_offset = sdata[1] * 8;
1754         //mame_printf_debug("Write EEPROM: offset %02X: ", block_offset);
1755         for (i=0; i < 8; i++)
1632         UINT8 block_offset = sdata[1] * 8;
1633         for(int i = 0; i < 8; i++)
17561634         {
1757            //mame_printf_debug("%02X ", sdata[2+i]);
17581635            eeprom[block_offset+i] = sdata[2+i];
17591636         }
1760         //mame_printf_debug("\n");
17611637
1762         //rdata[0] = 0;
1763
17641638         return 1;
17651639      }
17661640
r14029r14030
17821656   return 0;
17831657}
17841658
1785static void handle_pif(running_machine &machine)
1659void n64_periphs::handle_pif()
17861660{
1787   int j;
1788
1789   /*
1790    {
1791        int i;
1792        for (i=0; i < 8; i++)
1793        {
1794            int j = i * 8;
1795            mame_printf_debug("PIFCMD%d: %02X %02X %02X %02X %02X %02X %02X %02X\n", i, pif_cmd[j], pif_cmd[j+1], pif_cmd[j+2], pif_cmd[j+3], pif_cmd[j+4], pif_cmd[j+5], pif_cmd[j+6], pif_cmd[j+7]);
1796        }
1797        mame_printf_debug("\n");
1798    }
1799    */
1800
1801   if (pif_cmd[0x3f] == 0x1)      // only handle the command if the last byte is 1
1661   if(pif_cmd[0x3f] == 0x1)      // only handle the command if the last byte is 1
18021662   {
18031663      int channel = 0;
18041664      int end = 0;
18051665      int cmd_ptr = 0;
18061666
1807      while (cmd_ptr < 0x3f && !end)
1667      while(cmd_ptr < 0x3f && !end)
18081668      {
1809         UINT8 bytes_to_send;
1810         INT8 bytes_to_recv;
1669         UINT8 bytes_to_send = pif_cmd[cmd_ptr++];
18111670
1812         bytes_to_send = pif_cmd[cmd_ptr++];
1813
18141671         if (bytes_to_send == 0xfe)
18151672         {
18161673            end = 1;
r14029r14030
18211678         }
18221679         else
18231680         {
1824            if (bytes_to_send > 0 && (bytes_to_send & 0xc0) == 0)
1681            if(bytes_to_send > 0 && (bytes_to_send & 0xc0) == 0)
18251682            {
1826               int res;
18271683               UINT8 recv_buffer[0x40];
18281684               UINT8 send_buffer[0x40];
18291685
1830               bytes_to_recv = pif_cmd[cmd_ptr++];
1686               UINT8 bytes_to_recv = pif_cmd[cmd_ptr++];
18311687
1832               for (j=0; j < bytes_to_send; j++)
1688               for(int j = 0; j < bytes_to_send; j++)
18331689               {
18341690                  send_buffer[j] = pif_cmd[cmd_ptr++];
18351691               }
18361692
1837                    res = pif_channel_handle_command(machine, channel, bytes_to_send, send_buffer, bytes_to_recv, recv_buffer);
1693                    int res = pif_channel_handle_command(channel, bytes_to_send, send_buffer, bytes_to_recv, recv_buffer);
18381694
18391695               if (res == 0)
18401696               {
r14029r14030
18421698                  {
18431699                     fatalerror("cmd_ptr overflow\n");
18441700                  }
1845                  for (j=0; j < bytes_to_recv; j++)
1701                  for(int j = 0; j < bytes_to_recv; j++)
18461702                  {
18471703                     pif_ram[cmd_ptr++] = recv_buffer[j];
18481704                  }
r14029r14030
18601716
18611717      pif_ram[0x3f] = 0;
18621718   }
1863
1864   /*
1865    {
1866        int i;
1867        for (i=0; i < 8; i++)
1868        {
1869            int j = i * 8;
1870            mame_printf_debug("PIFRAM%d: %02X %02X %02X %02X %02X %02X %02X %02X\n", i, pif_ram[j], pif_ram[j+1], pif_ram[j+2], pif_ram[j+3], pif_ram[j+4], pif_ram[j+5], pif_ram[j+6], pif_ram[j+7]);
1871        }
1872        mame_printf_debug("\n");
1873    }
1874    */
18751719}
18761720
1877static void pif_dma(running_machine &machine, int direction)
1721void n64_periphs::pif_dma(int direction)
18781722{
1879   int i;
18801723   UINT32 *src, *dst;
18811724
18821725   if (si_dram_addr & 0x3)
r14029r14030
18881731   {
18891732      src = (UINT32*)&rdram[(si_dram_addr & 0x1fffffff) / 4];
18901733
1891      for (i=0; i < 64; i+=4)
1734      for(int i = 0; i < 64; i+=4)
18921735      {
18931736         UINT32 d = *src++;
18941737         pif_ram[i+0] = (d >> 24) & 0xff;
r14029r14030
19011744   }
19021745   else            // PIF RAM -> RDRAM
19031746   {
1904      handle_pif(machine);
1747      handle_pif();
19051748
19061749      dst = (UINT32*)&rdram[(si_dram_addr & 0x1fffffff) / 4];
19071750
1908      for (i=0; i < 64; i+=4)
1751      for(int i = 0; i < 64; i+=4)
19091752      {
19101753         UINT32 d = 0;
19111754         d |= pif_ram[i+0] << 24;
r14029r14030
19181761   }
19191762
19201763   si_status |= 0x1000;
1921   signal_rcp_interrupt(machine, SI_INTERRUPT);
1764   signal_rcp_interrupt(SI_INTERRUPT);
19221765}
19231766
1924READ32_HANDLER( n64_si_reg_r )
1767READ32_MEMBER( n64_periphs::si_reg_r )
19251768{
1769   //printf("si_reg_r %08x\n", offset * 4);
19261770   switch (offset)
19271771   {
19281772      //case 0x00/4:      // SI_DRAM_ADDR_REG
r14029r14030
19341778   return 0;
19351779}
19361780
1937WRITE32_HANDLER( n64_si_reg_w )
1781WRITE32_MEMBER( n64_periphs::si_reg_w )
19381782{
1783   //printf("si_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
19391784   switch (offset)
19401785   {
19411786      case 0x00/4:      // SI_DRAM_ADDR_REG
19421787         si_dram_addr = data;
1943   //      mame_printf_debug("si_dram_addr = %08X\n", si_dram_addr);
19441788         break;
19451789
19461790      case 0x04/4:      // SI_PIF_ADDR_RD64B_REG
19471791         // PIF RAM -> RDRAM
19481792         si_pif_addr = data;
19491793            si_pif_addr_rd64b = data;
1950            pif_dma(space->machine(), 0);
1794            pif_dma(0);
19511795         break;
19521796
19531797      case 0x10/4:      // SI_PIF_ADDR_WR64B_REG
19541798         // RDRAM -> PIF RAM
19551799         si_pif_addr = data;
19561800            si_pif_addr_wr64b = data;
1957            pif_dma(space->machine(), 1);
1801            pif_dma(1);
19581802         break;
19591803
19601804      case 0x18/4:      // SI_STATUS_REG
19611805         si_status &= ~0x1000;
1962         clear_rcp_interrupt(space->machine(), SI_INTERRUPT);
1806         clear_rcp_interrupt(SI_INTERRUPT);
19631807         break;
19641808
19651809      default:
r14029r14030
19681812   }
19691813}
19701814
1971static UINT32 cic_status = 0x00000000;
1972
1973READ32_HANDLER( n64_pif_ram_r )
1815READ32_MEMBER( n64_periphs::pif_ram_r )
19741816{
1975    /*mame_printf_debug( "pif_ram_r: %08X, %08X = %08X\n", offset << 2, mem_mask, ( ( pif_ram[offset*4+0] << 24 ) | ( pif_ram[offset*4+1] << 16 ) | ( pif_ram[offset*4+2] <<  8 ) | ( pif_ram[offset*4+3] <<  0 ) ) & mem_mask );*/
1976    if(!space->debugger_access())
1817    if(!space.debugger_access())
19771818    {
19781819       if( offset == ( 0x24 / 4 ) )
19791820       {
r14029r14030
19871828    return ( ( pif_ram[offset*4+0] << 24 ) | ( pif_ram[offset*4+1] << 16 ) | ( pif_ram[offset*4+2] <<  8 ) | ( pif_ram[offset*4+3] <<  0 ) ) & mem_mask;
19881829}
19891830
1990WRITE32_HANDLER( n64_pif_ram_w )
1831WRITE32_MEMBER( n64_periphs::pif_ram_w )
19911832{
1992    /*mame_printf_debug("pif_ram_w: %08X, %08X, %08X\n", data, offset << 4, mem_mask);*/
19931833    if( mem_mask & 0xff000000 )
19941834    {
19951835        pif_ram[offset*4+0] = ( data >> 24 ) & 0x000000ff;
r14029r14030
20071847        pif_ram[offset*4+3] = ( data >>  0 ) & 0x000000ff;
20081848    }
20091849
2010    signal_rcp_interrupt(space->machine(), SI_INTERRUPT);
1850    signal_rcp_interrupt(SI_INTERRUPT);
20111851}
20121852
20131853//static UINT16 crc_seed = 0x3f;
20141854
20151855MACHINE_START( n64 )
20161856{
2017   mips3drc_set_options(machine.device("maincpu"), MIPS3DRC_FASTEST_OPTIONS + MIPS3DRC_STRICT_VERIFY);
1857   mips3drc_set_options(machine.device("maincpu"), MIPS3DRC_FASTEST_OPTIONS + MIPS3DRC_STRICT_VERIFY + MIPS3DRC_STRICT_COP1);
20181858
20191859   /* configure fast RAM regions for DRC */
20201860   mips3drc_add_fastram(machine.device("maincpu"), 0x00000000, 0x007fffff, FALSE, rdram);
20211861
20221862   rspdrc_set_options(machine.device("rsp"), RSPDRC_STRICT_VERIFY);
20231863   rspdrc_flush_drc_cache(machine.device("rsp"));
2024
2025   audio_timer = machine.scheduler().timer_alloc(FUNC(audio_timer_callback));
20261864}
20271865
20281866MACHINE_RESET( n64 )
20291867{
2030   int i;
2031   UINT32 *cart = (UINT32*)machine.region("user2")->base();
2032   UINT64 boot_checksum;
2033
2034   mi_version = 0;
2035   mi_interrupt = 0;
2036   mi_intr_mask = 0;
2037   mi_mode = 0;
2038
2039   sp_mem_addr = 0;
2040   sp_dram_addr = 0;
2041   sp_dma_length = 0;
2042   sp_dma_count = 0;
2043   sp_dma_skip = 0;
2044   sp_semaphore = 0;
2045
2046   n64_vi_width = 0;
2047   n64_vi_origin = 0;
2048   n64_vi_control = 0;
2049   n64_vi_burst = n64_vi_vsync = n64_vi_hsync = n64_vi_leap = n64_vi_hstart = n64_vi_vstart = 0;
2050   n64_vi_intr = n64_vi_vburst = n64_vi_xscale = n64_vi_yscale = 0;
2051
2052   ai_dram_addr = 0;
2053   ai_len = 0 ;
2054   ai_control = 0;
2055   ai_dacrate = 0;
2056   ai_bitrate = 0;
2057   ai_status = 0;
2058
2059   memset(audio_fifo, 0, sizeof(audio_fifo));
2060   audio_fifo_wpos = 0;
2061   audio_fifo_rpos = 0;
2062   audio_fifo_num = 0;
2063
2064   pi_dram_addr = 0;
2065   pi_cart_addr = 0;
2066   pi_first_dma = 1;
2067
2068   memset(pif_ram, 0, sizeof(pif_ram));
2069   memset(pif_cmd, 0, sizeof(pif_cmd));
2070   si_dram_addr = 0;
2071   si_pif_addr = 0;
2072   si_status = 0;
2073
2074   memset(eeprom, 0, sizeof(eeprom));
2075   memset(mempack, 0, sizeof(mempack));
2076
2077   cic_status = 0;
2078
2079   audio_timer->adjust(attotime::never);
2080
20811868   cputag_set_input_line(machine, "rsp", INPUT_LINE_HALT, ASSERT_LINE);
2082
2083    // bootcode differs between CIC-chips, so we can use its checksum to detect the CIC-chip
2084    boot_checksum = 0;
2085    for (i=0x40; i < 0x1000; i+=4)
2086    {
2087        boot_checksum += cart[i/4]+i;
2088    }
2089
2090    if (boot_checksum == U64(0x000000d057e84864))
2091    {
2092        // CIC-NUS-6101
2093        printf("CIC-NUS-6102 detected\n");
2094        pif_ram[0x24] = 0x00;
2095        pif_ram[0x25] = 0x02;
2096        pif_ram[0x26] = 0x3f;
2097        pif_ram[0x27] = 0x3f;
2098        // crc_seed = 0x3f;
2099    }
2100    else if (boot_checksum == U64(0x000000cffb830843) || boot_checksum == U64(0x000000d0027fdf31))
2101    {
2102        // CIC-NUS-6103
2103        printf("CIC-NUS-6101 detected\n");
2104        // crc_seed = 0x78;
2105        pif_ram[0x24] = 0x00;
2106        pif_ram[0x25] = 0x06;
2107        pif_ram[0x26] = 0x3f;
2108        pif_ram[0x27] = 0x3f;
2109    }
2110    else if (boot_checksum == U64(0x000000d6499e376b))
2111    {
2112        // CIC-NUS-6103
2113        printf("CIC-NUS-6103 detected\n");
2114        // crc_seed = 0x78;
2115        pif_ram[0x24] = 0x00;
2116        pif_ram[0x25] = 0x02;
2117        pif_ram[0x26] = 0x78;
2118        pif_ram[0x27] = 0x3f;
2119    }
2120    else if (boot_checksum == U64(0x0000011a4a1604b6))
2121    {
2122        // CIC-NUS-6105
2123        printf("CIC-NUS-6105 detected\n");
2124        // crc_seed = 0x91;
2125
2126        // first_rsp = 0;
2127        pif_ram[0x24] = 0x00;
2128        pif_ram[0x25] = 0x02;
2129        pif_ram[0x26] = 0x91;
2130        pif_ram[0x27] = 0x3f;
2131    }
2132    else if (boot_checksum == U64(0x000000d6d5de4ba0))
2133    {
2134        // CIC-NUS-6106
2135        printf("CIC-NUS-6106 detected\n");
2136        // crc_seed = 0x85;
2137        pif_ram[0x24] = 0x00;
2138        pif_ram[0x25] = 0x02;
2139        pif_ram[0x26] = 0x85;
2140        pif_ram[0x27] = 0x3f;
2141    }
2142    else
2143    {
2144        printf("Unknown BootCode Checksum %08X%08X\n", (UINT32)(boot_checksum>>32),(UINT32)(boot_checksum));
2145    }
21461869}
src/mame/includes/n64.h
r14029r14030
33
44#include "cpu/rsp/rsp.h"
55#include "video/n64.h"
6#include "sound/dmadac.h"
7#include "includes/n64.h"
68
9/*----------- forward decls -----------*/
10
711/*----------- driver state -----------*/
812
913class _n64_state : public driver_device
r14029r14030
1620   N64::RDP::Processor m_rdp;
1721};
1822
23/*----------- devices -----------*/
24
25#define MCFG_N64_PERIPHS_ADD(_tag) \
26    MCFG_DEVICE_ADD(_tag, N64PERIPH, 0)
27
28#define AUDIO_DMA_DEPTH     2
29
30class n64_periphs : public device_t
31{
32private:
33   typedef struct
34   {
35      UINT32 address;
36      UINT32 length;
37   } AUDIO_DMA;
38
39public:
40    // construction/destruction
41    n64_periphs(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
42
43   DECLARE_READ32_MEMBER( is64_r );
44   DECLARE_WRITE32_MEMBER( is64_w );
45   DECLARE_READ32_MEMBER( open_r );
46   DECLARE_WRITE32_MEMBER( open_w );
47   DECLARE_READ32_MEMBER( rdram_reg_r );
48   DECLARE_WRITE32_MEMBER( rdram_reg_w );
49   DECLARE_READ32_MEMBER( mi_reg_r );
50   DECLARE_WRITE32_MEMBER( mi_reg_w );
51   DECLARE_READ32_MEMBER( vi_reg_r );
52   DECLARE_WRITE32_MEMBER( vi_reg_w );
53   DECLARE_READ32_MEMBER( ai_reg_r );
54   DECLARE_WRITE32_MEMBER( ai_reg_w );
55   DECLARE_READ32_MEMBER( pi_reg_r );
56   DECLARE_WRITE32_MEMBER( pi_reg_w );
57   DECLARE_READ32_MEMBER( ri_reg_r );
58   DECLARE_WRITE32_MEMBER( ri_reg_w );
59   DECLARE_READ32_MEMBER( si_reg_r );
60   DECLARE_WRITE32_MEMBER( si_reg_w );
61   DECLARE_READ32_MEMBER( pif_ram_r );
62   DECLARE_WRITE32_MEMBER( pif_ram_w );
63
64   UINT32 sp_reg_r(UINT32 offset);
65   void sp_reg_w(UINT32 offset, UINT32 data, UINT32 mem_mask);
66
67   void sp_set_status(UINT32 status);
68   void signal_rcp_interrupt(int interrupt);
69
70   void ai_timer_tick();
71   void pi_dma_tick();
72
73   // Video Interface (VI) registers
74   UINT32 vi_width;
75   UINT32 vi_origin;
76   UINT32 vi_control;
77   UINT32 vi_blank;
78   UINT32 vi_hstart;
79   UINT32 vi_vstart;
80   UINT32 vi_xscale;
81   UINT32 vi_yscale;
82   UINT32 vi_burst;
83   UINT32 vi_vsync;
84   UINT32 vi_hsync;
85   UINT32 vi_leap;
86   UINT32 vi_intr;
87   UINT32 vi_vburst;
88
89protected:
90    // device-level overrides
91    virtual void device_start();
92    virtual void device_reset();
93
94private:
95   address_space *mem_map;
96   device_t *maincpu;
97   device_t *rspcpu;
98
99   void clear_rcp_interrupt(int interrupt);
100
101   UINT8 is64_buffer[0x10000];
102
103   // Audio Interface (AI) registers and functions
104   void ai_dma();
105   AUDIO_DMA *ai_fifo_get_top();
106   void ai_fifo_push(UINT32 address, UINT32 length);
107   void ai_fifo_pop();
108
109   dmadac_sound_device *ai_dac[2];
110   UINT32 ai_dram_addr;
111   UINT32 ai_len;
112   UINT32 ai_control;
113   int ai_dacrate;
114   int ai_bitrate;
115   UINT32 ai_status;
116
117   emu_timer *ai_timer;
118
119   AUDIO_DMA ai_fifo[AUDIO_DMA_DEPTH];
120   int ai_fifo_wpos;
121   int ai_fifo_rpos;
122   int ai_fifo_num;
123
124   // Memory Interface (MI) registers
125   UINT32 mi_version;
126   UINT32 mi_interrupt;
127   UINT32 mi_intr_mask;
128   UINT32 mi_mode;
129
130   // RDRAM Interface (RI) registers
131   UINT32 rdram_regs[10];
132   UINT32 ri_regs[8];
133
134   // RSP Interface (SP) registers
135   void sp_dma(int direction);
136
137   UINT32 sp_mem_addr;
138   UINT32 sp_dram_addr;
139   int sp_dma_length;
140   int sp_dma_count;
141   int sp_dma_skip;
142   UINT32 sp_semaphore;
143   UINT32 dp_clock;
144
145   // Peripheral Interface (PI) registers and functions
146   void pi_dma();
147   emu_timer *pi_dma_timer;
148   UINT32 pi_dram_addr;
149   UINT32 pi_cart_addr;
150   UINT32 pi_first_dma;
151   UINT32 pi_rd_len;
152   UINT32 pi_wr_len;
153   UINT32 pi_status;
154   UINT32 pi_bsd_dom1_lat;
155   UINT32 pi_bsd_dom1_pwd;
156   UINT32 pi_bsd_dom1_pgs;
157   UINT32 pi_bsd_dom1_rls;
158   UINT32 pi_bsd_dom2_lat;
159   UINT32 pi_bsd_dom2_pwd;
160   UINT32 pi_bsd_dom2_pgs;
161   UINT32 pi_bsd_dom2_rls;
162   UINT32 pi_dma_dir;
163
164   // Serial Interface (SI) registers and functions
165   void pif_dma(int direction);
166   void handle_pif();
167   int pif_channel_handle_command(int channel, int slength, UINT8 *sdata, int rlength, UINT8 *rdata);
168   UINT8 calc_mempack_crc(UINT8 *buffer, int length);
169   UINT8 pif_ram[0x40];
170   UINT8 pif_cmd[0x40];
171   UINT32 si_dram_addr;
172   UINT32 si_pif_addr;
173   UINT32 si_pif_addr_rd64b;
174   UINT32 si_pif_addr_wr64b;
175   UINT32 si_status;
176   UINT8 eeprom[512];
177   UINT8 mempack[0x8000];
178   UINT32 cic_status;
179
180   // Video Interface (VI) functions
181   void vi_recalculate_resolution();
182};
183
184// device type definition
185extern const device_type N64PERIPH;
186
19187/*----------- defined in video/n64.c -----------*/
20188
21189extern VIDEO_START( n64 );
r14029r14030
25193#define DACRATE_PAL   (49656530)
26194#define DACRATE_MPAL   (48628316)
27195
196/*----------- defined in machine/n64.c -----------*/
197
28198#define SP_INTERRUPT   0x1
29199#define SI_INTERRUPT   0x2
30200#define AI_INTERRUPT   0x4
r14029r14030
52222#define DP_STATUS_FREEZE      0x02
53223#define DP_STATUS_FLUSH         0x04
54224
55/*----------- defined in machine/n64.c -----------*/
56
57225extern const rsp_config n64_rsp_config;
58226
59227extern UINT32 *rdram;
60228extern UINT32 *rsp_imem;
61229extern UINT32 *rsp_dmem;
62230
63extern UINT32 n64_vi_width;
64extern UINT32 n64_vi_origin;
65extern UINT32 n64_vi_control;
66extern UINT32 n64_vi_blank;
67extern UINT32 n64_vi_hstart;
68extern UINT32 n64_vi_vstart;
69extern UINT32 n64_vi_xscale;
70extern UINT32 n64_vi_yscale;
71
72231extern void dp_full_sync(running_machine &machine);
73232extern void signal_rcp_interrupt(running_machine &machine, int interrupt);
74extern void clear_rcp_interrupt(running_machine &machine, int interrupt);
75233
76
77/* read/write handlers for the N64 subsystems */
78extern READ32_HANDLER( n64_is64_r );
79extern WRITE32_HANDLER( n64_is64_w );
80extern READ32_HANDLER( n64_open_r );
81extern WRITE32_HANDLER( n64_open_w );
82extern READ32_HANDLER( n64_rdram_reg_r );
83extern WRITE32_HANDLER( n64_rdram_reg_w );
84extern READ32_HANDLER( n64_mi_reg_r );
85extern WRITE32_HANDLER( n64_mi_reg_w );
86234extern READ32_DEVICE_HANDLER( n64_sp_reg_r );
87235extern WRITE32_DEVICE_HANDLER( n64_sp_reg_w );
88236extern READ32_DEVICE_HANDLER( n64_dp_reg_r );
89237extern WRITE32_DEVICE_HANDLER( n64_dp_reg_w );
90extern READ32_HANDLER( n64_vi_reg_r );
91extern WRITE32_HANDLER( n64_vi_reg_w );
92extern READ32_HANDLER( n64_ai_reg_r );
93extern WRITE32_HANDLER( n64_ai_reg_w );
94extern READ32_HANDLER( n64_pi_reg_r );
95extern WRITE32_HANDLER( n64_pi_reg_w );
96extern READ32_HANDLER( n64_ri_reg_r );
97extern WRITE32_HANDLER( n64_ri_reg_w );
98extern READ32_HANDLER( n64_si_reg_r );
99extern WRITE32_HANDLER( n64_si_reg_w );
100extern READ32_HANDLER( n64_pif_ram_r );
101extern WRITE32_HANDLER( n64_pif_ram_w );
102238
103239MACHINE_START( n64 );
104240MACHINE_RESET( n64 );
src/mame/video/n64.c
r14029r14030
6666
6767/*****************************************************************************/
6868
69void Processor::VideoUpdate(bitmap_rgb32 &bitmap)
69void Processor::VideoUpdate(n64_periphs *n64, bitmap_rgb32 &bitmap)
7070{
71   switch(n64_vi_control & 0x3)
71   switch(n64->vi_control & 0x3)
7272   {
7373      case PIXEL_SIZE_16BIT:
74         VideoUpdate16(bitmap);
74         VideoUpdate16(n64, bitmap);
7575         break;
7676
7777      case PIXEL_SIZE_32BIT:
78         VideoUpdate32(bitmap);
78         VideoUpdate32(n64, bitmap);
7979         break;
8080
8181      default:
r14029r14030
8484   }
8585}
8686
87void Processor::VideoUpdate16(bitmap_rgb32 &bitmap)
87void Processor::VideoUpdate16(n64_periphs *n64, bitmap_rgb32 &bitmap)
8888{
89    int fsaa = (((n64_vi_control >> 8) & 3) < 2);
90    int divot = (n64_vi_control >> 4) & 1;
89    int fsaa = (((n64->vi_control >> 8) & 3) < 2);
90    int divot = (n64->vi_control >> 4) & 1;
9191
9292   UINT32 prev_cvg = 0;
9393   UINT32 next_cvg = 0;
94    //int dither_filter = (n64_vi_control >> 16) & 1;
95    //int vibuffering = ((n64_vi_control & 2) && fsaa && divot);
94    //int dither_filter = (n64->vi_control >> 16) & 1;
95    //int vibuffering = ((n64->vi_control & 2) && fsaa && divot);
9696
97   UINT16 *frame_buffer = (UINT16*)&rdram[(n64_vi_origin & 0xffffff) >> 2];
98   UINT32 hb = ((n64_vi_origin & 0xffffff) >> 2) >> 1;
97   UINT16 *frame_buffer = (UINT16*)&rdram[(n64->vi_origin & 0xffffff) >> 2];
98   UINT32 hb = ((n64->vi_origin & 0xffffff) >> 2) >> 1;
9999   UINT8* hidden_buffer = &m_hidden_bits[hb];
100100
101   INT32 hdiff = (n64_vi_hstart & 0x3ff) - ((n64_vi_hstart >> 16) & 0x3ff);
102   float hcoeff = ((float)(n64_vi_xscale & 0xfff) / (1 << 10));
101   INT32 hdiff = (n64->vi_hstart & 0x3ff) - ((n64->vi_hstart >> 16) & 0x3ff);
102   float hcoeff = ((float)(n64->vi_xscale & 0xfff) / (1 << 10));
103103   UINT32 hres = ((float)hdiff * hcoeff);
104   INT32 invisiblewidth = n64_vi_width - hres;
104   INT32 invisiblewidth = n64->vi_width - hres;
105105
106   INT32 vdiff = ((n64_vi_vstart & 0x3ff) - ((n64_vi_vstart >> 16) & 0x3ff)) >> 1;
107   float vcoeff = ((float)(n64_vi_yscale & 0xfff) / (1 << 10));
106   INT32 vdiff = ((n64->vi_vstart & 0x3ff) - ((n64->vi_vstart >> 16) & 0x3ff)) >> 1;
107   float vcoeff = ((float)(n64->vi_yscale & 0xfff) / (1 << 10));
108108   UINT32 vres = ((float)vdiff * vcoeff);
109109
110110   if (vdiff <= 0 || hdiff <= 0)
r14029r14030
150150            {
151151               //if (/*!vibuffering &&*/ state->m_rdp.GetMiscState()->m_curpixel_cvg < 7 && i > 1 && j > 1 && i < (hres - 2) && j < (vres - 2))
152152               //{
153                  //video_filter16(&c.i.r, &c.i.g, &c.i.b, &frame_buffer[pixels ^ WORD_ADDR_XOR],&hidden_buffer[pixels ^ BYTE_ADDR_XOR], n64_vi_width);
153                  //video_filter16(&c.i.r, &c.i.g, &c.i.b, &frame_buffer[pixels ^ WORD_ADDR_XOR],&hidden_buffer[pixels ^ BYTE_ADDR_XOR], n64->vi_width);
154154               //}
155155            }
156156            //else if (dither_filter && state->m_rdp.GetMiscState()->m_curpixel_cvg == 7 && i > 0 && j > 0 && i < (hres - 1) && j < (vres - 1))
157157            //{
158158               //if (vibuffering)
159159               //{
160               //  restore_filter16_buffer(&r, &g, &b, &ViBuffer[i][j], n64_vi_width);
160               //  restore_filter16_buffer(&r, &g, &b, &ViBuffer[i][j], n64->vi_width);
161161               //}
162162               //else
163163               //{
164                  //restore_filter16(&c.i.r, &c.i.g, &c.i.b, &frame_buffer[pixels ^ WORD_ADDR_XOR], pixels ^ WORD_ADDR_XOR, n64_vi_width);
164                  //restore_filter16(&c.i.r, &c.i.g, &c.i.b, &frame_buffer[pixels ^ WORD_ADDR_XOR], pixels ^ WORD_ADDR_XOR, n64->vi_width);
165165               //}
166166            //}
167167            if(divot)
r14029r14030
218218   }
219219}
220220
221void Processor::VideoUpdate32(bitmap_rgb32 &bitmap)
221void Processor::VideoUpdate32(n64_periphs *n64, bitmap_rgb32 &bitmap)
222222{
223    int gamma = (n64_vi_control >> 3) & 1;
224    int gamma_dither = (n64_vi_control >> 2) & 1;
225    //int vibuffering = ((n64_vi_control & 2) && fsaa && divot);
223    int gamma = (n64->vi_control >> 3) & 1;
224    int gamma_dither = (n64->vi_control >> 2) & 1;
225    //int vibuffering = ((n64->vi_control & 2) && fsaa && divot);
226226
227    UINT32 *frame_buffer32 = (UINT32*)&rdram[(n64_vi_origin & 0xffffff) >> 2];
227    UINT32 *frame_buffer32 = (UINT32*)&rdram[(n64->vi_origin & 0xffffff) >> 2];
228228
229   const INT32 hdiff = (n64_vi_hstart & 0x3ff) - ((n64_vi_hstart >> 16) & 0x3ff);
230   const float hcoeff = ((float)(n64_vi_xscale & 0xfff) / (1 << 10));
229   const INT32 hdiff = (n64->vi_hstart & 0x3ff) - ((n64->vi_hstart >> 16) & 0x3ff);
230   const float hcoeff = ((float)(n64->vi_xscale & 0xfff) / (1 << 10));
231231   UINT32 hres = ((float)hdiff * hcoeff);
232   INT32 invisiblewidth = n64_vi_width - hres;
232   INT32 invisiblewidth = n64->vi_width - hres;
233233
234   const INT32 vdiff = ((n64_vi_vstart & 0x3ff) - ((n64_vi_vstart >> 16) & 0x3ff)) >> 1;
235   const float vcoeff = ((float)(n64_vi_yscale & 0xfff) / (1 << 10));
234   const INT32 vdiff = ((n64->vi_vstart & 0x3ff) - ((n64->vi_vstart >> 16) & 0x3ff)) >> 1;
235   const float vcoeff = ((float)(n64->vi_yscale & 0xfff) / (1 << 10));
236236   const UINT32 vres = ((float)vdiff * vcoeff);
237237
238238   if (vdiff <= 0 || hdiff <= 0)
r14029r14030
32703270SCREEN_UPDATE_RGB32(n64)
32713271{
32723272   _n64_state *state = screen.machine().driver_data<_n64_state>();
3273   n64_periphs *n64 = screen.machine().device<n64_periphs>("rcp");
32733274
32743275    int height = state->m_rdp.GetMiscState()->m_fb_height;
3275   //UINT16 *frame_buffer = (UINT16*)&rdram[(n64_vi_origin & 0xffffff) >> 2];
3276   //UINT8  *cvg_buffer = &state->m_rdp.GetHiddenBits()[((n64_vi_origin & 0xffffff) >> 2) >> 1];
3277    //int vibuffering = ((n64_vi_control & 2) && fsaa && divot);
3276   //UINT16 *frame_buffer = (UINT16*)&rdram[(n64->vi_origin & 0xffffff) >> 2];
3277   //UINT8  *cvg_buffer = &state->m_rdp.GetHiddenBits()[((n64->vi_origin & 0xffffff) >> 2) >> 1];
3278    //int vibuffering = ((n64->vi_control & 2) && fsaa && divot);
32783279
32793280   //vibuffering = 0; // Disabled for now
32803281
32813282   /*
3282    if (vibuffering && ((n64_vi_control & 3) == 2))
3283    if (vibuffering && ((n64->vi_control & 3) == 2))
32833284    {
32843285        if (frame_buffer)
32853286        {
r14029r14030
32923293                    curpixel_cvg = ((pix & 1) << 2) | (cvg_buffer[pixels ^ BYTE_ADDR_XOR] & 3); // Reuse of this variable
32933294                    if (curpixel_cvg < 7 && i > 1 && j > 1 && i < (hres - 2) && j < (vres - 2) && fsaa)
32943295                    {
3295                        newc = video_filter16(&frame_buffer[pixels ^ WORD_ADDR_XOR], &cvg_buffer[pixels ^ BYTE_ADDR_XOR], n64_vi_width);
3296                        newc = video_filter16(&frame_buffer[pixels ^ WORD_ADDR_XOR], &cvg_buffer[pixels ^ BYTE_ADDR_XOR], n64->vi_width);
32963297                        ViBuffer[i][j] = newc;
32973298                    }
32983299                    else
r14029r14030
33103311    }
33113312    */
33123313
3313    if (n64_vi_blank)
3314    if (n64->vi_blank)
33143315    {
33153316        for (int j = 0; j <height; j++)
33163317        {
r14029r14030
33233324        return 0;
33243325    }
33253326
3326   state->m_rdp.VideoUpdate(bitmap);
3327   state->m_rdp.VideoUpdate(n64, bitmap);
33273328
33283329   return 0;
33293330}
src/mame/video/n64.h
r14029r14030
8585
8686/*****************************************************************************/
8787
88class n64_periphs;
89
8890namespace N64
8991{
9092
r14029r14030
574576      bool         ZCompare(UINT32 zcurpixel, UINT32 dzcurpixel, UINT32 sz, UINT16 dzpix);
575577
576578      // Fullscreen update-related
577      void         VideoUpdate(bitmap_rgb32 &bitmap);
579      void         VideoUpdate(n64_periphs *n64, bitmap_rgb32 &bitmap);
578580
579581      // Commands
580582      void      CmdInvalid(UINT32 w1, UINT32 w2);
r14029r14030
754756      static const ZDecompressEntry z_dec_table[8];
755757
756758      // Internal screen-update functions
757      void         VideoUpdate16(bitmap_rgb32 &bitmap);
758      void         VideoUpdate32(bitmap_rgb32 &bitmap);
759      void         VideoUpdate16(n64_periphs *n64, bitmap_rgb32 &bitmap);
760      void         VideoUpdate32(n64_periphs *n64, bitmap_rgb32 &bitmap);
759761
760762      typedef void (*Command)(UINT32 w1, UINT32 w2);
761763
src/mess/drivers/n64.c
r14029r14030
1717
1818static ADDRESS_MAP_START( n64_map, AS_PROGRAM, 32 )
1919   AM_RANGE(0x00000000, 0x007fffff) AM_RAM   AM_BASE(&rdram)            // RDRAM
20   AM_RANGE(0x03f00000, 0x03f00027) AM_READWRITE(n64_rdram_reg_r, n64_rdram_reg_w)
20   AM_RANGE(0x03f00000, 0x03f00027) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, rdram_reg_r, rdram_reg_w)
2121   AM_RANGE(0x04000000, 0x04000fff) AM_RAM AM_SHARE("dmem")               // RSP DMEM
2222   AM_RANGE(0x04001000, 0x04001fff) AM_RAM AM_SHARE("imem")               // RSP IMEM
2323   AM_RANGE(0x04040000, 0x040fffff) AM_DEVREADWRITE("rsp", n64_sp_reg_r, n64_sp_reg_w)   // RSP
2424   AM_RANGE(0x04100000, 0x041fffff) AM_DEVREADWRITE("rsp", n64_dp_reg_r, n64_dp_reg_w)   // RDP
25   AM_RANGE(0x04300000, 0x043fffff) AM_READWRITE(n64_mi_reg_r, n64_mi_reg_w)   // MIPS Interface
26   AM_RANGE(0x04400000, 0x044fffff) AM_READWRITE(n64_vi_reg_r, n64_vi_reg_w)   // Video Interface
27   AM_RANGE(0x04500000, 0x045fffff) AM_READWRITE(n64_ai_reg_r, n64_ai_reg_w)   // Audio Interface
28   AM_RANGE(0x04600000, 0x046fffff) AM_READWRITE(n64_pi_reg_r, n64_pi_reg_w)   // Peripheral Interface
29   AM_RANGE(0x04700000, 0x047fffff) AM_READWRITE(n64_ri_reg_r, n64_ri_reg_w)   // RDRAM Interface
30   AM_RANGE(0x04800000, 0x048fffff) AM_READWRITE(n64_si_reg_r, n64_si_reg_w)   // Serial Interface
25   AM_RANGE(0x04300000, 0x043fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, mi_reg_r, mi_reg_w)   // MIPS Interface
26   AM_RANGE(0x04400000, 0x044fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, vi_reg_r, vi_reg_w)   // Video Interface
27   AM_RANGE(0x04500000, 0x045fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, ai_reg_r, ai_reg_w)   // Audio Interface
28   AM_RANGE(0x04600000, 0x046fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, pi_reg_r, pi_reg_w)   // Peripheral Interface
29   AM_RANGE(0x04700000, 0x047fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, ri_reg_r, ri_reg_w)   // RDRAM Interface
30   AM_RANGE(0x04800000, 0x048fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, si_reg_r, si_reg_w)   // Serial Interface
3131   //AM_RANGE(0x08000000, 0x08007fff) AM_RAM                                   // Cartridge SRAM
3232   AM_RANGE(0x10000000, 0x13ffffff) AM_ROM AM_REGION("user2", 0)   // Cartridge
3333   AM_RANGE(0x1fc00000, 0x1fc007bf) AM_ROM AM_REGION("user1", 0)   // PIF ROM
34   AM_RANGE(0x1fc007c0, 0x1fc007ff) AM_READWRITE(n64_pif_ram_r, n64_pif_ram_w)
34   AM_RANGE(0x1fc007c0, 0x1fc007ff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, pif_ram_r, pif_ram_w)
3535ADDRESS_MAP_END
3636
3737static ADDRESS_MAP_START( rsp_map, AS_PROGRAM, 32 )
r14029r14030
186186   MCFG_SOUND_ADD("dac1", DMADAC, 0)
187187   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "rspeaker", 1.0)
188188
189   MCFG_N64_PERIPHS_ADD("rcp");
190
189191   /* cartridge */
190192   MCFG_CARTSLOT_ADD("cart")
191193   MCFG_CARTSLOT_EXTENSION_LIST("v64,z64,rom,n64,bin")

Previous 15994 Revisions Next


© 1998-2018 The MESS Team