Previous 15994 Revisions Next

r12441 Monday 1st August, 2011 at 12:09:49 UTC by Tim Schuerewegen
h8: rewrote H8S/2xxx 8-bit and 16-bit timers. [Tim Schuerewegen]
[src/emu/cpu/h83002]h8.h h8_16.c h8priv.h h8speriph.c

src/emu/cpu/h83002/h8.h
r12440r12441
181181#define H8S_IO_TSTR   H8S_IO(0xFFC0)
182182#define H8S_IO_TSYR   H8S_IO(0xFFC1)
183183// TPU0
184#define H8S_IO_TTCR0  H8S_IO(0xFFD0)
185#define H8S_IO_TMDR0  H8S_IO(0xFFD1)
186#define H8S_IO_TIOR0H H8S_IO(0xFFD2)
187#define H8S_IO_TIOR0L H8S_IO(0xFFD3)
188#define H8S_IO_TIER0  H8S_IO(0xFFD4)
189#define H8S_IO_TSR0   H8S_IO(0xFFD5)
190#define H8S_IO_TTCNT0 H8S_IO(0xFFD6)
191#define H8S_IO_TGR0A  H8S_IO(0xFFD8)
192#define H8S_IO_TGR0B  H8S_IO(0xFFDA)
193#define H8S_IO_TGR0C  H8S_IO(0xFFDC)
194#define H8S_IO_TGR0D  H8S_IO(0xFFDE)
184#define H8S_IO_TTCR0      H8S_IO(0xFFD0)
185#define H8S_IO_TMDR0      H8S_IO(0xFFD1)
186#define H8S_IO_TIOR0_H      H8S_IO(0xFFD2)
187#define H8S_IO_TIOR0_L      H8S_IO(0xFFD3)
188#define H8S_IO_TIER0      H8S_IO(0xFFD4)
189#define H8S_IO_TSR0         H8S_IO(0xFFD5)
190#define H8S_IO_TCNT0_H      H8S_IO(0xFFD6)
191#define H8S_IO_TCNT0_L      H8S_IO(0xFFD7)
192#define H8S_IO_TGR0A_H      H8S_IO(0xFFD8)
193#define H8S_IO_TGR0A_L      H8S_IO(0xFFD9)
194#define H8S_IO_TGR0B_H      H8S_IO(0xFFDA)
195#define H8S_IO_TGR0B_L      H8S_IO(0xFFDB)
196#define H8S_IO_TGR0C_H      H8S_IO(0xFFDC)
197#define H8S_IO_TGR0C_L      H8S_IO(0xFFDD)
198#define H8S_IO_TGR0D_H      H8S_IO(0xFFDE)
199#define H8S_IO_TGR0D_L      H8S_IO(0xFFDF)
195200// TPU1
196#define H8S_IO_TTCR1  H8S_IO(0xFFE0)
197#define H8S_IO_TMDR1  H8S_IO(0xFFE1)
198#define H8S_IO_TIOR1  H8S_IO(0xFFE2)
199#define H8S_IO_TIER1  H8S_IO(0xFFE4)
200#define H8S_IO_TSR1   H8S_IO(0xFFE5)
201#define H8S_IO_TTCNT1 H8S_IO(0xFFE6)
202#define H8S_IO_TGR1A  H8S_IO(0xFFE8)
203#define H8S_IO_TGR1B  H8S_IO(0xFFEA)
201#define H8S_IO_TTCR1      H8S_IO(0xFFE0)
202#define H8S_IO_TMDR1      H8S_IO(0xFFE1)
203#define H8S_IO_TIOR1      H8S_IO(0xFFE2)
204#define H8S_IO_TIER1      H8S_IO(0xFFE4)
205#define H8S_IO_TSR1         H8S_IO(0xFFE5)
206#define H8S_IO_TCNT1_H      H8S_IO(0xFFE6)
207#define H8S_IO_TCNT1_L      H8S_IO(0xFFE7)
208#define H8S_IO_TGR1A_H      H8S_IO(0xFFE8)
209#define H8S_IO_TGR1A_L      H8S_IO(0xFFE9)
210#define H8S_IO_TGR1B_H      H8S_IO(0xFFEA)
211#define H8S_IO_TGR1B_L      H8S_IO(0xFFEB)
212// TPU2
213#define H8S_IO_TTCR2      H8S_IO(0xFFF0)
214#define H8S_IO_TMDR2      H8S_IO(0xFFF1)
215#define H8S_IO_TIOR2      H8S_IO(0xFFF2)
216#define H8S_IO_TIER2      H8S_IO(0xFFF4)
217#define H8S_IO_TSR2         H8S_IO(0xFFF5)
218#define H8S_IO_TCNT2_H      H8S_IO(0xFFF6)
219#define H8S_IO_TCNT2_L      H8S_IO(0xFFF7)
220#define H8S_IO_TGR2A_H      H8S_IO(0xFFF8)
221#define H8S_IO_TGR2A_L      H8S_IO(0xFFF9)
222#define H8S_IO_TGR2B_H      H8S_IO(0xFFFA)
223#define H8S_IO_TGR2B_L      H8S_IO(0xFFFB)
224// TPU3
225#define H8S_IO_TTCR3      H8S_IO(0xFE80)
226#define H8S_IO_TMDR3      H8S_IO(0xFE81)
227#define H8S_IO_TIOR3_H      H8S_IO(0xFE82)
228#define H8S_IO_TIOR3_L      H8S_IO(0xFE83)
229#define H8S_IO_TIER3      H8S_IO(0xFE84)
230#define H8S_IO_TSR3         H8S_IO(0xFE85)
231#define H8S_IO_TCNT3_H      H8S_IO(0xFE86)
232#define H8S_IO_TCNT3_L      H8S_IO(0xFE87)
233#define H8S_IO_TGR3A_H      H8S_IO(0xFE88)
234#define H8S_IO_TGR3A_L      H8S_IO(0xFE89)
235#define H8S_IO_TGR3B_H      H8S_IO(0xFE8A)
236#define H8S_IO_TGR3B_L      H8S_IO(0xFE8B)
237#define H8S_IO_TGR3C_H      H8S_IO(0xFE8C)
238#define H8S_IO_TGR3C_L      H8S_IO(0xFE8D)
239#define H8S_IO_TGR3D_H      H8S_IO(0xFE8E)
240#define H8S_IO_TGR3D_L      H8S_IO(0xFE8F)
241// TPU4
242#define H8S_IO_TTCR4      H8S_IO(0xFE90)
243#define H8S_IO_TMDR4      H8S_IO(0xFE91)
244#define H8S_IO_TIOR4      H8S_IO(0xFE92)
245#define H8S_IO_TIER4      H8S_IO(0xFE94)
246#define H8S_IO_TSR4         H8S_IO(0xFE95)
247#define H8S_IO_TCNT4_H      H8S_IO(0xFE96)
248#define H8S_IO_TCNT4_L      H8S_IO(0xFE97)
249#define H8S_IO_TGR4A_H      H8S_IO(0xFE98)
250#define H8S_IO_TGR4A_L      H8S_IO(0xFE99)
251#define H8S_IO_TGR4B_H      H8S_IO(0xFE9A)
252#define H8S_IO_TGR4B_L      H8S_IO(0xFE9B)
204253// TPU5
205#define H8S_IO_TTCR5  H8S_IO(0xFEA0)
206#define H8S_IO_TMDR5  H8S_IO(0xFEA1)
207#define H8S_IO_TIOR5  H8S_IO(0xFEA2)
208#define H8S_IO_TIER5  H8S_IO(0xFEA4)
209#define H8S_IO_TSR5   H8S_IO(0xFEA5)
210#define H8S_IO_TTCNT5 H8S_IO(0xFEA6)
211#define H8S_IO_TGR5A  H8S_IO(0xFEA8)
212#define H8S_IO_TGR5B  H8S_IO(0xFEAA)
254#define H8S_IO_TTCR5      H8S_IO(0xFEA0)
255#define H8S_IO_TMDR5      H8S_IO(0xFEA1)
256#define H8S_IO_TIOR5      H8S_IO(0xFEA2)
257#define H8S_IO_TIER5      H8S_IO(0xFEA4)
258#define H8S_IO_TSR5         H8S_IO(0xFEA5)
259#define H8S_IO_TCNT5_H      H8S_IO(0xFEA6)
260#define H8S_IO_TCNT5_L      H8S_IO(0xFEA7)
261#define H8S_IO_TGR5A_H      H8S_IO(0xFEA8)
262#define H8S_IO_TGR5A_L      H8S_IO(0xFEA9)
263#define H8S_IO_TGR5B_H      H8S_IO(0xFEAA)
264#define H8S_IO_TGR5B_L      H8S_IO(0xFEAB)
213265// DMA
214266#define H8S_IO_MAR1AH  H8S_IO(0xFEF0)
215267#define H8S_IO_MAR1AL  H8S_IO(0xFEF2)
r12440r12441
248300#define H8S_P5_RXD2 0x02
249301#define H8S_P5_TXD2 0x01
250302
303#define H8S_PF_PF6 0x40
251304#define H8S_PF_PF2 0x04
252305#define H8S_PF_PF1 0x02
253306#define H8S_PF_PF0 0x01
r12440r12441
294347#define H8S_INT_TGI0B   0x21
295348#define H8S_INT_TGI0C   0x22
296349#define H8S_INT_TGI0D   0x23
297#define H8S_INT_TGI0V   0x24
350#define H8S_INT_TCI0V   0x24
298351#define H8S_INT_TGI1A   0x28
299352#define H8S_INT_TGI1B   0x29
300#define H8S_INT_TGI1V   0x2A
301#define H8S_INT_TGI1U   0x2B
353#define H8S_INT_TCI1V   0x2A
354#define H8S_INT_TCI1U   0x2B
302355#define H8S_INT_TGI2A   0x2C
303356#define H8S_INT_TGI2B   0x2D
304#define H8S_INT_TGI2V   0x2E
305#define H8S_INT_TGI2U   0x2F
357#define H8S_INT_TCI2V   0x2E
358#define H8S_INT_TCI2U   0x2F
306359#define H8S_INT_CMIA0   0x40
307360#define H8S_INT_CMIB0   0x41
308361#define H8S_INT_OVI0    0x42
r12440r12441
322375#define H8S_INT_TXI2    0x5A
323376#define H8S_INT_TEI2    0x5B
324377
378#define H8S_INT_TGI3A   48
379#define H8S_INT_TGI3B   49
380#define H8S_INT_TGI3C   50
381#define H8S_INT_TGI3D   51
382#define H8S_INT_TCI3V   52
383#define H8S_INT_TGI4A   56
384#define H8S_INT_TGI4B   57
385#define H8S_INT_TCI4V   58
386#define H8S_INT_TCI4U   59
387#define H8S_INT_TGI5A   60
388#define H8S_INT_TGI5B   61
389#define H8S_INT_TCI5V   62
390#define H8S_INT_TCI5U   63
391
325392DECLARE_LEGACY_CPU_DEVICE(H83002, h8_3002);
326393DECLARE_LEGACY_CPU_DEVICE(H83007, h8_3007);
327394DECLARE_LEGACY_CPU_DEVICE(H83044, h8_3044);
src/emu/cpu/h83002/h8_16.c
r12440r12441
578578   }
579579}
580580
581WRITE16_HANDLER( h8s2241_per_regs_w )
582{
583   h83xx_state *h8 = get_safe_token(&space->device());
584   if (mem_mask == 0xffff)
585   {
586      h8s2241_per_regs_write_16(h8, (offset << 1), data);
587   }
588   else if (mem_mask & 0xff00)
589   {
590      h8s2241_per_regs_write_8(h8, (offset << 1), (data >> 8) & 0xff);
591   }
592   else if (mem_mask == 0x00ff)
593   {
594      h8s2241_per_regs_write_8(h8, (offset << 1) + 1, data & 0xff);
595   }
596}
597
598WRITE16_HANDLER( h8s2246_per_regs_w )
599{
600   h83xx_state *h8 = get_safe_token(&space->device());
601   if (mem_mask == 0xffff)
602   {
603      h8s2246_per_regs_write_16(h8, (offset << 1), data);
604   }
605   else if (mem_mask == 0xff00)
606   {
607      h8s2246_per_regs_write_8(h8, (offset << 1), (data >> 8) & 0xff);
608   }
609   else if (mem_mask == 0x00ff)
610   {
611      h8s2246_per_regs_write_8(h8, (offset << 1) + 1, data & 0xff);
612   }
613}
614
615WRITE16_HANDLER( h8s2323_per_regs_w )
616{
617   h83xx_state *h8 = get_safe_token(&space->device());
618   if (mem_mask == 0xffff)
619   {
620      h8s2323_per_regs_write_16(h8, (offset << 1), data);
621   }
622   else if (mem_mask & 0xff00)
623   {
624      h8s2323_per_regs_write_8(h8, (offset << 1), (data >> 8) & 0xff);
625   }
626   else if (mem_mask == 0x00ff)
627   {
628      h8s2323_per_regs_write_8(h8, (offset << 1) + 1, data & 0xff);
629   }
630}
631
632READ16_HANDLER( h8s2241_per_regs_r )
633{
634   h83xx_state *h8 = get_safe_token(&space->device());
635   if (mem_mask == 0xffff)
636   {
637      return h8s2241_per_regs_read_16(h8, (offset << 1));
638   }
639   else if (mem_mask == 0xff00)
640   {
641      return h8s2241_per_regs_read_8(h8, (offset << 1)) << 8;
642   }
643   else if (mem_mask == 0x00ff)
644   {
645      return h8s2241_per_regs_read_8(h8, (offset << 1) + 1);
646   }
647   return 0;
648}
649
650READ16_HANDLER( h8s2246_per_regs_r )
651{
652   h83xx_state *h8 = get_safe_token(&space->device());
653   if (mem_mask == 0xffff)
654   {
655      return h8s2246_per_regs_read_16(h8, (offset << 1));
656   }
657   else if (mem_mask == 0xff00)
658   {
659      return h8s2246_per_regs_read_8(h8, (offset << 1)) << 8;
660   }
661   else if (mem_mask == 0x00ff)
662   {
663      return h8s2246_per_regs_read_8(h8, (offset << 1) + 1);
664   }
665   return 0;
666}
667
668READ16_HANDLER( h8s2323_per_regs_r )
669{
670   h83xx_state *h8 = get_safe_token(&space->device());
671   if (mem_mask == 0xffff)
672   {
673      return h8s2323_per_regs_read_16(h8, (offset << 1));
674   }
675   else if (mem_mask == 0xff00)
676   {
677      return h8s2323_per_regs_read_8(h8, (offset << 1)) << 8;
678   }
679   else if (mem_mask == 0x00ff)
680   {
681      return h8s2323_per_regs_read_8(h8, (offset << 1) + 1);
682   }
683   return 0;
684}
685
581686// On-board RAM and peripherals
582687static ADDRESS_MAP_START( h8_3002_internal_map, AS_PROGRAM, 16 )
583688   // 512B RAM
r12440r12441
599704ADDRESS_MAP_END
600705
601706static ADDRESS_MAP_START( h8s_2241_internal_map, AS_PROGRAM, 16 )
602   AM_RANGE( 0xFFDC00, 0xFFFBFF ) AM_RAM // on-chip ram
603   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE8( h8s2241_per_regs_r_byte, h8s2241_per_regs_w_byte, 0xffff ) // internal i/o registers
707   AM_RANGE( 0xFFEC00, 0xFFFBFF ) AM_RAM // on-chip ram
708   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE( h8s2241_per_regs_r, h8s2241_per_regs_w ) // internal i/o registers
604709ADDRESS_MAP_END
605710
606711static ADDRESS_MAP_START( h8s_2246_internal_map, AS_PROGRAM, 16 )
607712   AM_RANGE( 0xFFDC00, 0xFFFBFF ) AM_RAM // on-chip ram
608   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE8( h8s2246_per_regs_r_byte, h8s2246_per_regs_w_byte, 0xffff ) // internal i/o registers
713   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE( h8s2246_per_regs_r, h8s2246_per_regs_w ) // internal i/o registers
609714ADDRESS_MAP_END
610715
611716static ADDRESS_MAP_START( h8s_2323_internal_map, AS_PROGRAM, 16 )
612717   AM_RANGE( 0xFFDC00, 0xFFFBFF ) AM_RAM // on-chip ram
613   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE8( h8s2323_per_regs_r_byte, h8s2323_per_regs_w_byte, 0xffff ) // internal i/o registers
718   AM_RANGE( 0xFFFE40, 0xFFFFFF ) AM_READWRITE( h8s2323_per_regs_r, h8s2323_per_regs_w ) // internal i/o registers
614719ADDRESS_MAP_END
615720
616721CPU_GET_INFO( h8_3002 )
src/emu/cpu/h83002/h8priv.h
r12440r12441
1717typedef struct
1818{
1919   emu_timer *timer;
20   H8S2XXX_TPU_ITEM item[8];
21   int prescaler, tgrmax, tgrcur;
20   int cycles_per_tick;
21   UINT64 timer_cycles;
2222} H8S2XXX_TPU;
2323
2424typedef struct
r12440r12441
3030typedef struct
3131{
3232   emu_timer *timer;
33   int cycles_per_tick;
34   UINT64 timer_cycles;
3335} H8S2XXX_TMR;
3436
3537typedef struct _h83xx_state h83xx_state;
r12440r12441
6870   emu_timer *frctimer;
6971
7072   H8S2XXX_TMR tmr[2];
71   H8S2XXX_TPU tpu[3];
73   H8S2XXX_TPU tpu[6];
7274   H8S2XXX_SCI sci[3];
7375
7476   int mode_8bit;
r12440r12441
110112void h8_itu_write8(h83xx_state *h8, UINT8 reg, UINT8 val);
111113void h8_3007_itu_write8(h83xx_state *h8, UINT8 reg, UINT8 val);
112114
113READ8_HANDLER( h8s2241_per_regs_r_byte );
114WRITE8_HANDLER( h8s2241_per_regs_w_byte );
115READ8_HANDLER( h8s2246_per_regs_r_byte );
116WRITE8_HANDLER( h8s2246_per_regs_w_byte );
117READ8_HANDLER( h8s2323_per_regs_r_byte );
118WRITE8_HANDLER( h8s2323_per_regs_w_byte );
115UINT8 h8s2241_per_regs_read_8(h83xx_state *h8, int offset);
116UINT8 h8s2246_per_regs_read_8(h83xx_state *h8, int offset);
117UINT8 h8s2323_per_regs_read_8(h83xx_state *h8, int offset);
119118
119UINT16 h8s2241_per_regs_read_16(h83xx_state *h8, int offset);
120UINT16 h8s2246_per_regs_read_16(h83xx_state *h8, int offset);
121UINT16 h8s2323_per_regs_read_16(h83xx_state *h8, int offset);
122
123void h8s2241_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data);
124void h8s2246_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data);
125void h8s2323_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data);
126
127void h8s2241_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data);
128void h8s2246_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data);
129void h8s2323_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data);
130
120131#endif /* __H8PRIV_H__ */
src/emu/cpu/h83002/h8speriph.c
r12440r12441
1515#include "h8.h"
1616#include "h8priv.h"
1717
18#define LOG_LEVEL  1
19#define _logerror(level,...)  if (LOG_LEVEL > level) logerror(__VA_ARGS__)
18#define VERBOSE_LEVEL ( 0 )
2019
21//#define ENABLE_SCI_TIMER
20INLINE void ATTR_PRINTF(3,4) verboselog( running_machine &machine, int n_level, const char *s_fmt, ...)
21{
22   if (VERBOSE_LEVEL >= n_level)
23   {
24      va_list v;
25      char buf[32768];
26      va_start( v, s_fmt);
27      vsprintf( buf, s_fmt, v);
28      va_end( v);
29      logerror( "%s: %s", machine.describe_context( ), buf);
30   }
31}
2232
33const UINT8 H8S_RESET_H8S_IO_224x[0x1C0] = // values from "e602100_h8s2245.pdf"
34{
35   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE40
36   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE50
37   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE60
38   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE70
39   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE80
40   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE90
41   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEA0
42   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // FEB0
43   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEC0
44   0xFF, 0xFF, 0xFF, 0xFF, 0xD0, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FED0
45   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEE0
46   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEF0
47   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF00
48   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF10
49   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF20
50   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, // FF30
51   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF40
52   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF50
53   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF60
54   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF70
55   0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF80
56   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF90
57   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFA0
58   0x00, 0x00, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFB0
59   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFC0
60   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FFD0
61   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFE0
62   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFF0
63};
64
65const UINT8 H8S_RESET_H8S_IO_2323[0x1C0] = // values from "rej09b0220_h8s2329.pdf"
66{
67   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE40
68   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE50
69   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE60
70   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE70
71   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FE80
72   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FE90
73   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FEA0
74   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // FEB0
75   0x00, 0x00, 0x00, 0x00, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x00, // FEC0
76   0xFF, 0xFF, 0xFF, 0xFF, 0xD0, 0x3C, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FED0
77   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEE0
78   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEF0
79   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF00
80   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF10
81   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF20
82   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, // FF30
83   0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF40
84   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF50
85   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF60
86   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF70
87   0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF80
88   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF90
89   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, // FFA0
90   0x00, 0x00, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFB0
91   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFC0
92   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FFD0
93   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFE0
94   0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFF0
95};
96
97#define SCR_TIE      0x80
98#define SCR_RIE      0x40
99#define SCR_TE      0x20
100#define SCR_RE      0x10
101
102#define SSR_TDRE  0x80
103#define SSR_RDRF  0x40
104#define SSR_ORER  0x20
105#define SSR_FER   0x10
106#define SSR_PER   0x08
107#define SSR_TEND  0x04
108
109#define SMR_CM    0x80
110#define SMR_CHR   0x40
111#define SMR_PE    0x20
112#define SMR_PM    0x10
113#define SMR_STOP  0x08
114#define SMR_MP    0x04
115#define SMR_CKS1  0x02
116#define SMR_CKS0  0x01
117
118#define TCSR_CMFB  0x80
119#define TCSR_CMFA  0x40
120#define TCSR_OVF   0x20
121
122#define TCR_CMIEB  0x80
123#define TCR_CMIEA  0x40
124#define TCR_OVIE   0x20
125
126#define TSR_TCFD  0x80
127#define TSR_TCFU  0x20
128#define TSR_TCFV  0x10
129#define TSR_TGFD  0x08
130#define TSR_TGFC  0x04
131#define TSR_TGFB  0x02
132#define TSR_TGFA  0x01
133
134#define TIER_TCIEU  0x20
135#define TIER_TCIEV  0x10
136#define TIER_TGIED  0x08
137#define TIER_TGIEC  0x04
138#define TIER_TGIEB  0x02
139#define TIER_TGIEA  0x01
140
141#define DTC_MRA_MD_NORMAL  0
142#define DTC_MRA_MD_REPEAT  1
143#define DTC_MRA_MD_BLOCK   2
144
145#define DTC_MRA_SM1  0x80
146#define DTC_MRA_SM0  0x40
147#define DTC_MRA_DM1  0x20
148#define DTC_MRA_DM0  0x10
149#define DTC_MRA_DTS  0x02
150
151#define DTC_MRB_CHNE   0x80
152#define DTC_MRB_DISEL  0x40
153#define DTC_MRB_CHNS   0x20
154
155#define DMABCRL_DTME1   0x80
156#define DMABCRL_DTE1    0x40
157#define DMABCRL_DTME0   0x20
158#define DMABCRL_DTE0    0x10
159#define DMABCRL_DTIE1B  0x08
160#define DMABCRL_DTIE1A  0x04
161#define DMABCRL_DTIE0B  0x02
162#define DMABCRL_DTIE0A  0x01
163
164#define ADCSR_ADF   0x80
165#define ADCSR_ADIE  0x40
166#define ADCSR_ADST  0x20
167#define ADCSR_SCAN  0x10
168
169enum
170{
171   PIN_P10, PIN_P11, PIN_P12, PIN_P13, PIN_P14, PIN_P15, PIN_P16, PIN_P17,
172   PIN_P20, PIN_P21, PIN_P22, PIN_P23, PIN_P24, PIN_P25, PIN_P26, PIN_P27,
173   PIN_P30, PIN_P31, PIN_P32, PIN_P33, PIN_P34, PIN_P35,
174   PIN_P40, PIN_P41, PIN_P42, PIN_P43, PIN_P44, PIN_P45, PIN_P46, PIN_P47,
175   PIN_P50, PIN_P51, PIN_P52, PIN_P53,
176   PIN_P60, PIN_P61, PIN_P62, PIN_P63, PIN_P64, PIN_P65, PIN_P66, PIN_P67,
177   PIN_PA0, PIN_PA1, PIN_PA2, PIN_PA3, PIN_PA4, PIN_PA5, PIN_PA6, PIN_PA7,
178   PIN_PB0, PIN_PB1, PIN_PB2, PIN_PB3, PIN_PB4, PIN_PB5, PIN_PB6, PIN_PB7,
179   PIN_PC0, PIN_PC1, PIN_PC2, PIN_PC3, PIN_PC4, PIN_PC5, PIN_PC6, PIN_PC7,
180   PIN_PD0, PIN_PD1, PIN_PD2, PIN_PD3, PIN_PD4, PIN_PD5, PIN_PD6, PIN_PD7,
181   PIN_PE0, PIN_PE1, PIN_PE2, PIN_PE3, PIN_PE4, PIN_PE5, PIN_PE6, PIN_PE7,
182   PIN_PF0, PIN_PF1, PIN_PF2, PIN_PF3, PIN_PF4, PIN_PF5, PIN_PF6, PIN_PF7,
183   PIN_PG0, PIN_PG1, PIN_PG2, PIN_PG3, PIN_PG4
184};
185
186#define PIN_TIOCA0   PIN_P10
187#define PIN_TIOCB0   PIN_P11
188#define PIN_TIOCC0   PIN_P12
189#define PIN_TIOCD0   PIN_P13
190#define PIN_TIOCA1   PIN_P14
191#define PIN_TIOCB1   PIN_P15
192#define PIN_TIOCA2   PIN_P16
193#define PIN_TIOCB2   PIN_P17
194
195#define PIN_TCLKA   PIN_P12
196#define PIN_TCLKB   PIN_P13
197#define PIN_TCLKC   PIN_P15
198#define PIN_TCLKD   PIN_P17
199
200#define PIN_TXD0   PIN_P30
201#define PIN_TXD1   PIN_P31
202#define PIN_RXD0   PIN_P32
203#define PIN_RXD1   PIN_P33
204#define PIN_SCK0   PIN_P34
205#define PIN_SCK1   PIN_P35
206
207#define PIN_TIOCA3   PIN_P20
208#define PIN_TIOCB3   PIN_P21
209#define PIN_TIOCC3   PIN_P22
210#define PIN_TIOCD3   PIN_P23
211#define PIN_TIOCA4   PIN_P24
212#define PIN_TIOCB4   PIN_P25
213#define PIN_TIOCA5   PIN_P26
214#define PIN_TIOCB5   PIN_P27
215
216#define PIN_TMRI0   PIN_P22
217#define PIN_TMCI0   PIN_P23
218#define PIN_TMRI1   PIN_P24
219#define PIN_TMCI1   PIN_P25
220#define PIN_TMO0   PIN_P26
221#define PIN_TMO1   PIN_P27
222
223const int TPU_0_DIV[] = { 1, 4, 16, 64, 0, 0, 0,0 };
224const int TPU_1_DIV[] = { 1, 4, 16, 64, 0, 0, 256, 0 };
225const int TPU_2_DIV[] = { 1, 4, 16, 64, 0, 0, 0, 1024 };
226const int TPU_3_DIV[] = { 1, 4, 16, 64, 0, 1024, 256, 4096 };
227const int TPU_4_DIV[] = { 1, 4, 16, 64, 0, 0, 1024, 0 };
228const int TPU_5_DIV[] = { 1, 4, 16, 64, 0, 0, 256, 0 };
229
230const int H8S_IO_SMR[] = { H8S_IO_SMR0, H8S_IO_SMR1, H8S_IO_SMR2 };
231const int H8S_IO_BRR[] = { H8S_IO_BRR0, H8S_IO_BRR1, H8S_IO_BRR2 };
232const int H8S_IO_SCR[] = { H8S_IO_SCR0, H8S_IO_SCR1, H8S_IO_SCR2 };
233const int H8S_IO_TDR[] = { H8S_IO_TDR0, H8S_IO_TDR1, H8S_IO_TDR2 };
234const int H8S_IO_SSR[] = { H8S_IO_SSR0, H8S_IO_SSR1, H8S_IO_SSR2 };
235const int H8S_IO_RDR[] = { H8S_IO_RDR0, H8S_IO_RDR1, H8S_IO_RDR2 };
236
237const int H8S_INT_TXI[] = { H8S_INT_TXI0, H8S_INT_TXI1, H8S_INT_TXI2 };
238const int H8S_INT_RXI[] = { H8S_INT_RXI0, H8S_INT_RXI1, H8S_INT_RXI2 };
239
240const int H8S_IO_TCORA[] = { H8S_IO_TCORA0, H8S_IO_TCORA1 };
241const int H8S_IO_TCORB[] = { H8S_IO_TCORB0, H8S_IO_TCORB1 };
242const int H8S_IO_TCR[]   = { H8S_IO_TCR0,   H8S_IO_TCR1   };
243const int H8S_IO_TCNT[]  = { H8S_IO_TCNT0,  H8S_IO_TCNT1  };
244const int H8S_IO_TCSR[]  = { H8S_IO_TCSR0,  H8S_IO_TCSR1  };
245
246const int H8S_INT_OVI[]  = { H8S_INT_OVI0,  H8S_INT_OVI1  };
247const int H8S_INT_CMIA[] = { H8S_INT_CMIA0, H8S_INT_CMIA1 };
248const int H8S_INT_CMIB[] = { H8S_INT_CMIB0, H8S_INT_CMIB1 };
249
250const int H8S_IO_TGRA_H[] = { H8S_IO_TGR0A_H, H8S_IO_TGR1A_H, H8S_IO_TGR2A_H, H8S_IO_TGR3A_H, H8S_IO_TGR4A_H, H8S_IO_TGR5A_H };
251const int H8S_IO_TGRA_L[] = { H8S_IO_TGR0A_L, H8S_IO_TGR1A_L, H8S_IO_TGR2A_L, H8S_IO_TGR3A_L, H8S_IO_TGR4A_L, H8S_IO_TGR5A_L };
252const int H8S_IO_TGRB_H[] = { H8S_IO_TGR0B_H, H8S_IO_TGR1B_H, H8S_IO_TGR2B_H, H8S_IO_TGR3B_H, H8S_IO_TGR4B_H, H8S_IO_TGR5B_H };
253const int H8S_IO_TGRB_L[] = { H8S_IO_TGR0B_L, H8S_IO_TGR1B_L, H8S_IO_TGR2B_L, H8S_IO_TGR3B_L, H8S_IO_TGR4B_L, H8S_IO_TGR5B_L };
254const int H8S_IO_TGRC_H[] = { H8S_IO_TGR0C_H, 0, 0, H8S_IO_TGR3C_H, 0, 0 };
255const int H8S_IO_TGRC_L[] = { H8S_IO_TGR0C_L, 0, 0, H8S_IO_TGR3C_L, 0, 0 };
256const int H8S_IO_TGRD_H[] = { H8S_IO_TGR0D_H, 0, 0, H8S_IO_TGR3D_H, 0, 0 };
257const int H8S_IO_TGRD_L[] = { H8S_IO_TGR0D_L, 0, 0, H8S_IO_TGR3D_L, 0, 0 };
258const int H8S_IO_TCNT_H[] = { H8S_IO_TCNT0_H, H8S_IO_TCNT1_H, H8S_IO_TCNT2_H, H8S_IO_TCNT3_H, H8S_IO_TCNT4_H, H8S_IO_TCNT5_H };
259const int H8S_IO_TCNT_L[] = { H8S_IO_TCNT0_L, H8S_IO_TCNT1_L, H8S_IO_TCNT2_L, H8S_IO_TCNT3_L, H8S_IO_TCNT4_L, H8S_IO_TCNT5_L };
260const int H8S_IO_TIOR_H[] = { H8S_IO_TIOR0_H, H8S_IO_TIOR1, H8S_IO_TIOR2, H8S_IO_TIOR3_H, H8S_IO_TIOR4, H8S_IO_TIOR5 };
261const int H8S_IO_TIOR_L[] = { H8S_IO_TIOR0_L, 0, 0, H8S_IO_TIOR3_L, 0, 0 };
262const int H8S_IO_TTCR[]   = { H8S_IO_TTCR0, H8S_IO_TTCR1, H8S_IO_TTCR2, H8S_IO_TTCR3, H8S_IO_TTCR4, H8S_IO_TTCR5 };
263const int H8S_IO_TIER[]   = { H8S_IO_TIER0, H8S_IO_TIER1, H8S_IO_TIER2, H8S_IO_TIER3, H8S_IO_TIER4, H8S_IO_TIER5 };
264const int H8S_IO_TSR[]    = { H8S_IO_TSR0, H8S_IO_TSR1, H8S_IO_TSR2, H8S_IO_TSR3, H8S_IO_TSR4, H8S_IO_TSR5 };
265
266const int H8S_INT_TCIV[] = { H8S_INT_TCI0V, H8S_INT_TCI1V, H8S_INT_TCI2V, H8S_INT_TCI3V, H8S_INT_TCI4V, H8S_INT_TCI5V };
267const int H8S_INT_TCIU[] = { 0, H8S_INT_TCI1U, H8S_INT_TCI2U, 0, H8S_INT_TCI4U, H8S_INT_TCI5U };
268const int H8S_INT_TGIA[] = { H8S_INT_TGI0A, H8S_INT_TGI1A, H8S_INT_TGI2A, H8S_INT_TGI3A, H8S_INT_TGI4A, H8S_INT_TGI5A };
269const int H8S_INT_TGIB[] = { H8S_INT_TGI0B, H8S_INT_TGI1B, H8S_INT_TGI2B, H8S_INT_TGI3B, H8S_INT_TGI4B, H8S_INT_TGI5B };
270const int H8S_INT_TGIC[] = { H8S_INT_TGI0C, 0, 0, H8S_INT_TGI3C, 0, 0 };
271const int H8S_INT_TGID[] = { H8S_INT_TGI0D, 0, 0, H8S_INT_TGI3D, 0, 0 };
272
273const int PIN_TIOCA[] = { PIN_TIOCA0, PIN_TIOCA1, PIN_TIOCA2, PIN_TIOCA3, PIN_TIOCA4, PIN_TIOCA5 };
274const int PIN_TIOCB[] = { PIN_TIOCB0, PIN_TIOCB1, PIN_TIOCB2, PIN_TIOCB3, PIN_TIOCB4, PIN_TIOCB5 };
275const int PIN_TIOCC[] = { PIN_TIOCC0, 0, 0, PIN_TIOCC3, 0, 0 };
276const int PIN_TIOCD[] = { PIN_TIOCD0, 0, 0, PIN_TIOCD3, 0, 0 };
277
278const int TCR_CCLR_AND[] = { 7, 3, 3, 7, 3, 3 };
279
280// prototypes
281
282void h8s_tmr_x_reset( h83xx_state *h8, int x);
283void h8s_tpu_x_reset( h83xx_state *h8, int x);
284void h8s_tpu_x_tick( h83xx_state *h8, int x);
285
23286////////////
24287// DTVECR //
25288////////////
r12440r12441
32295
33296static TIMER_CALLBACK( h8s_tmr_callback );
34297static TIMER_CALLBACK( h8s_tpu_callback );
35#ifdef ENABLE_SCI_TIMER
36298static TIMER_CALLBACK( h8s_sci_callback );
37#endif
38299
39300/////////////
40301// TIMER.C //
41302/////////////
42303
43#ifdef ENABLE_SCI_TIMER
44static void timer_adjust_pulse(emu_timer *timer, UINT32 hz, int num)
304UINT16 ptr_read_16( void *ptr)
45305{
46   timer->adjust(ATTOTIME_IN_HZ( hz), num, ATTOTIME_IN_HZ( hz));
306   return (*(((UINT8*)ptr) + 0) << 8) | (*(((UINT8*)ptr) + 1) << 0);
47307}
48#endif
49308
309void ptr_write_16( void *ptr, UINT16 data)
310{
311   *(((UINT8*)ptr) + 0) = (data >> 8) & 0xFF;
312   *(((UINT8*)ptr) + 1) = (data >> 0) & 0xFF;
313}
314
315int device_icount_min_timer_icount1( h83xx_state *h8, emu_timer *timer)
316{
317   return (timer->elapsed() * h8->device->unscaled_clock()).as_double();
318}
319
50320void h8s2xxx_interrupt_request(h83xx_state *h8, UINT32 vecnum)
51321{
52322   UINT8 idx, bit;
53323   idx = vecnum >> 5;
54324   bit = vecnum & 0x1F;
55325   h8->irq_req[idx] |= (1 << bit);
56//   logerror( "irq_req = %08X %08X %08X\n", h8->irq_req[0], h8->irq_req[1], h8->irq_req[2]);
57326}
58327
59328void h8s_dtce_execute(h83xx_state *h8, UINT32 addr_dtce, UINT8 mask_dtce, UINT32 addr_dtvecr)
r12440r12441
72341   dtc_dar   = (data[1] >>  0) & 0xFFFFFF;
73342   dtc_cra   = (data[2] >> 16) & 0xFFFF;
74343   dtc_crb   = (data[2] >>  0) & 0xFFFF;
75   _logerror( 3, "dtc : vect %08X mra %02X sar %08X mrb %02X dar %08X cra %04X crb %04X\n", dtc_vect, dtc_mra, dtc_sar, dtc_mrb, dtc_dar, dtc_cra, dtc_crb);
344   verboselog( h8->device->machine(), 3, "dtc : vect %08X mra %02X sar %08X mrb %02X dar %08X cra %04X crb %04X\n", dtc_vect, dtc_mra, dtc_sar, dtc_mrb, dtc_dar, dtc_cra, dtc_crb);
76345   // execute
77346   if ((dtc_mra & 0x0E) != 0x00) fatalerror("H8S: dtc unsupported MRA %x\n", dtc_mra&0x0e);
78347   sz = 1 << (dtc_mra & 0x01);
r12440r12441
135404
136405void h8s_periph_reset(h83xx_state *h8)
137406{
138   h8->per_regs[H8S_IO_SSR0] = 0x84;
139   h8->per_regs[H8S_IO_SSR1] = 0x84;
140   h8->per_regs[H8S_IO_SSR2] = 0x84;
141   h8->per_regs[H8S_IO_TDR0] = 0xFF;
142   h8->per_regs[H8S_IO_TDR1] = 0xFF;
143   h8->per_regs[H8S_IO_TDR2] = 0xFF;
144   h8->per_regs[H8S_IO_BRR0] = 0xFF;
145   h8->per_regs[H8S_IO_BRR1] = 0xFF;
146   h8->per_regs[H8S_IO_BRR2] = 0xFF;
407   const int tpu_max = (h8->device->type() == H8S2323) ? 6 : 3;
408   if ((h8->device->type() == H8S2241) || (h8->device->type() == H8S2246))
409   {
410      memcpy( h8->per_regs, H8S_RESET_H8S_IO_224x, sizeof( h8->per_regs));
411   }
412   else if (h8->device->type() == H8S2323)
413   {
414      memcpy( h8->per_regs, H8S_RESET_H8S_IO_2323, sizeof( h8->per_regs));
415   }
416   for (int i = 0; i < 2; i++) h8s_tmr_x_reset( h8, i);
417   for (int i = 0; i < tpu_max; i++) h8s_tpu_x_reset( h8, i);
147418}
148419
149///////////
150// TIMER //
151///////////
420/////////////////
421// TIMER 8-BIT //
422/////////////////
152423
153typedef struct
424void h8s_tmr_init(h83xx_state *h8)
154425{
155   UINT32 reg_tcsr, reg_tcr, reg_tcnt;
156   UINT8 int_ovi;
157} H8S_TMR_ENTRY;
426   for (int i = 0; i < 2; i++)
427   {
428      h8->tmr[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tmr_callback), h8);
429      h8->tmr[i].timer->adjust(attotime::never, i);
430   }
431}
158432
159const H8S_TMR_ENTRY H8S_TMR_TABLE[] =
433void h8s_tmr_x_reset( h83xx_state *h8, int x)
160434{
161   // TMR 0
435   H8S2XXX_TMR *tmr = &h8->tmr[x];
436   tmr->cycles_per_tick = 0;
437   tmr->timer->adjust(attotime::never, x);
438}
439
440UINT64 h8s_tmr_x_calculate_ticks( h83xx_state *h8, int x, UINT8 tcnt)
441{
442   UINT64 cycles1, cycles2;
443   UINT8 tcor;
444   // overflow
445   if (tcnt == 0) cycles1 = 0xFF + 1; else cycles1 = (UINT8)(0xFF - tcnt + 1);
446   // tcora
447   tcor = h8->per_regs[H8S_IO_TCORA[x]];
448   if (tcnt == tcor) cycles2 = 0xFF + 1; else cycles2 = (UINT8)(tcor - tcnt);
449   if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
450   // tcorb
451   tcor = h8->per_regs[H8S_IO_TCORB[x]];
452   if (tcnt == tcor) cycles2 = 0xFF + 1; else cycles2 = (UINT8)(tcor - tcnt);
453   if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
454   // done
455   return cycles1;
456}
457
458void h8s_tmr_x_update( h83xx_state *h8, int x)
459{
460   H8S2XXX_TMR *tmr = &h8->tmr[x];
461   UINT64 cycles;
462   int div = 0;
463   UINT8 tcr, tcnt;
464   tcr = h8->per_regs[H8S_IO_TCR[x]];
465   switch ((tcr >> 0) & 7)
162466   {
163      H8S_IO_TCSR0, H8S_IO_TCR0, H8S_IO_TCNT0,
164      H8S_INT_OVI0
165   },
166   // TMR 1
467      case 1  : div = 8; break;
468      case 2  : div = 64; break;
469      case 3  : div = 8192; break;
470//      default : OS_MessageBox( NULL, "tmr_x_update", "error", 0); break;
471   }
472   tmr->cycles_per_tick = div;
473   tcnt = h8->per_regs[H8S_IO_TCNT[x]];
474   verboselog( h8->device->machine(), 5, "TMR %d - update (div %d tcnt %02X)\n", x, div, tcnt);
475   // update timer
476   cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt) * tmr->cycles_per_tick;
477   tmr->timer_cycles = cycles;
478   tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
479}
480
481UINT8 h8s_tmr_x_calculate_tcnt( h83xx_state *h8, int x)
482{
483   H8S2XXX_TMR *tmr = &h8->tmr[x];
484   UINT8 tcnt;
485   tcnt = h8->per_regs[H8S_IO_TCNT[x]];
486   if (tmr->timer->enabled())
167487   {
168      H8S_IO_TCSR1, H8S_IO_TCR1, H8S_IO_TCNT1,
169      H8S_INT_OVI1
488      tcnt = (UINT8)(tcnt + ((device_icount_min_timer_icount1( h8, tmr->timer)) / tmr->cycles_per_tick));
170489   }
171};
490   return tcnt;
491}
172492
173const UINT32 TCR_CKS[] = { 0, 8, 64, 8192 };
174
175const H8S_TMR_ENTRY *h8s_tmr_entry( int num)
493void h8s_tmr_x_start( h83xx_state *h8, int x)
176494{
177   return &H8S_TMR_TABLE[num];
495   verboselog( h8->device->machine(), 5, "TMR %d - start\n", x);
496   // ...
497   h8s_tmr_x_update( h8, x);
178498}
179499
180static TIMER_CALLBACK( h8s_tmr_callback)
500void h8s_tmr_x_stop( h83xx_state *h8, int x)
181501{
182   h83xx_state *h8 = (h83xx_state *)ptr;
183   const H8S_TMR_ENTRY *info = h8s_tmr_entry( param);
184   _logerror( 2, "h8s_tmr_callback (%d)\n", param);
502   H8S2XXX_TMR *tmr = &h8->tmr[x];
503   UINT8 tcnt;
504   // ...
505   verboselog( h8->device->machine(), 5, "TMR %d - stop\n", x);
506   // calculate tcnt
507   tcnt = h8s_tmr_x_calculate_tcnt( h8, x);
508   // update tcnt
509   h8->per_regs[H8S_IO_TCNT[x]] = tcnt;
185510   // disable timer
186   h8->tmr[param].timer->adjust(attotime::never, param);
187   // set timer overflow flag
188   h8->per_regs[info->reg_tcsr] |= 0x20;
189   // overflow interrupt
190   if (h8->per_regs[info->reg_tcr] & 0x20) h8s2xxx_interrupt_request(h8, info->int_ovi);
511   tmr->timer->reset();
191512}
192513
193void h8s_tmr_init(h83xx_state *h8)
514UINT8 h8s_tmr_x_tcnt_change( h83xx_state *h8, int x, UINT8 tcnt)
194515{
195   int i;
196   for (i=0;i<2;i++)
516   UINT8 tcr, tcor;
517   int clear = 0;
518   // ...
519   verboselog( h8->device->machine(), 9, "TMR %d - tmr_x_tcnt_change (%02X)\n", x, tcnt);
520   // ...
521   tcr = h8->per_regs[H8S_IO_TCR[x]];
522   // overflow
523   if (tcnt == 0)
197524   {
198      h8->tmr[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tmr_callback), h8);
199      h8->tmr[i].timer->adjust(attotime::never, i);
525      verboselog( h8->device->machine(), 9, "TMR %d - overflow match\n", x);
526      h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_OVF;
527      if ((tcr & TCR_OVIE) != 0)
528      {
529         h8s2xxx_interrupt_request( h8, H8S_INT_OVI[x]);
530      }
200531   }
532   // tcora
533   tcor = h8->per_regs[H8S_IO_TCORA[x]];
534   if (tcnt == tcor)
535   {
536      verboselog( h8->device->machine(), 9, "TMR %d - tcora match\n", x);
537      h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_CMFA;
538      if ((tcr & TCR_CMIEA) != 0)
539      {
540         h8s2xxx_interrupt_request( h8, H8S_INT_CMIA[x]);
541      }
542      if (((tcr >> 3) & 3) == 1) // CCLR 1 -> "clear by compare match a"
543      {
544         clear = 1;
545      }
546   }
547   // tcorb
548   tcor = h8->per_regs[H8S_IO_TCORB[x]];
549   if (tcnt == tcor)
550   {
551      verboselog( h8->device->machine(), 9, "TMR %d - tcorb match\n", x);
552      h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_CMFB;
553      if ((tcr & TCR_CMIEB) != 0)
554      {
555         h8s2xxx_interrupt_request( h8, H8S_INT_CMIB[x]);
556      }
557      if (((tcr >> 3) & 3) == 2) // CCLR 2 -> "clear by compare match b"
558      {
559         clear = 1;
560      }
561   }
562   // ...
563   if (clear)
564   {
565      verboselog( h8->device->machine(), 5, "TMR %d - tcnt clear\n", x);
566      tcnt = 0;
567   }
568   // ...
569   return tcnt;
201570}
202571
203void h8s_tmr_resync(h83xx_state *h8, int num)
572void h8s_tmr_x_callback( h83xx_state *h8, int x)
204573{
205   const H8S_TMR_ENTRY *info = h8s_tmr_entry(num);
206   UINT8 cks, tcr, tcnt;
207   // get timer data
208   tcr = h8->per_regs[info->reg_tcr];
209   tcnt = h8->per_regs[info->reg_tcnt];
210   //
211   cks = tcr & 3;
212   if (cks != 0)
574   H8S2XXX_TMR *tmr = &h8->tmr[x];
575   UINT64 cycles;
576   UINT8 tcnt;
577   // calculate tcnt
578   tcnt = h8->per_regs[H8S_IO_TCNT[x]];
579   tcnt = (UINT8)(tcnt + tmr->timer_cycles / tmr->cycles_per_tick);
580   // ...
581   tcnt = h8s_tmr_x_tcnt_change( h8, x, tcnt);
582   // update tcnt
583   h8->per_regs[H8S_IO_TCNT[x]] = tcnt;
584   // update timer
585   cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt) * tmr->cycles_per_tick;
586   tmr->timer_cycles = cycles;
587   tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
588}
589
590void h8s_tmr_x_write_tcr( h83xx_state *h8, int x, UINT8 data)
591{
592   UINT8 old_data = h8->per_regs[H8S_IO_TCR[x]];
593   h8->per_regs[H8S_IO_TCR[x]] = data;
594   if ((data & 0x07) != (old_data & 0x07))
213595   {
214      UINT32 cycles, hz;
215      cycles = TCR_CKS[cks] * (0xFF - tcnt);
216      if (cycles == 0) cycles = 1; // cybiko game "lost in labyrinth" => divide by zero
217      hz = h8->device->unscaled_clock() / cycles;
218      _logerror( 2, "cycles %d hz %d\n", cycles, hz);
219      h8->tmr[num].timer->adjust(attotime::from_hz(hz), num);
596      if ((data & 0x07) != 0)
597      {
598         h8s_tmr_x_start( h8, x);
599      }
600      else
601      {
602         h8s_tmr_x_stop( h8, x);
603      }
220604   }
221   else
605}
606
607void h8s_tmr_x_write_tcnt( h83xx_state *h8, int x, UINT8 data)
608{
609   H8S2XXX_TMR *tmr = &h8->tmr[x];
610   h8->per_regs[H8S_IO_TCNT[x]] = data;
611   if (tmr->timer->enabled())
222612   {
223      _logerror( 2, "time never\n");
224      h8->tmr[num].timer->adjust(attotime::never, num);
613      h8s_tmr_x_update( h8, x);
225614   }
226615}
227616
228/////////
229// TPU //
230/////////
617void h8s_tmr_x_write_tcor( h83xx_state *h8, int x)
618{
619   H8S2XXX_TMR *tmr = &h8->tmr[x];
620   if ((tmr->timer->enabled()) && (tmr->cycles_per_tick != 0))
621   {
622      UINT8 tcnt1, tcnt2;
623      UINT64 cycles;
624      tcnt1 = h8->per_regs[H8S_IO_TCNT[x]];
625      tcnt2 = (UINT8)(tcnt1 + ((device_icount_min_timer_icount1( h8, tmr->timer)) / tmr->cycles_per_tick));
626      cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt2) * tmr->cycles_per_tick;
627      cycles = cycles + (tcnt2 - tcnt1) * tmr->cycles_per_tick;
628      tmr->timer_cycles = cycles;
629      tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
630   }
631}
231632
232const UINT32 TPSC[] = { 1, 4, 16, 64, 0, 0, 256, 0 };
633UINT8 h8s_tmr_x_read_tcnt( h83xx_state *h8, int x)
634{
635   return h8s_tmr_x_calculate_tcnt( h8, x);
636}
233637
234void set_p1dr_tiocb1(h83xx_state *h8, int data)
638void h8s_tmr_x_write_tcora( h83xx_state *h8, int x, UINT8 data)
235639{
236   UINT8 p1dr;
237   p1dr = h8->per_regs[H8S_IO_P1DR];
238   if (data) p1dr |= H8S_P1_TIOCB1; else p1dr &= ~H8S_P1_TIOCB1;
239   h8->io->write_byte(H8S_IO_ADDR(H8S_IO_P1DR), p1dr);
240   h8->per_regs[H8S_IO_P1DR] = p1dr;
640   h8->per_regs[H8S_IO_TCORA[x]] = data;
641   h8s_tmr_x_write_tcor( h8, x);
241642}
242643
243static TIMER_CALLBACK( h8s_tpu_callback)
644void h8s_tmr_x_write_tcorb( h83xx_state *h8, int x, UINT8 data)
244645{
646   h8->per_regs[H8S_IO_TCORB[x]] = data;
647   h8s_tmr_x_write_tcor( h8, x);
648}
649
650static TIMER_CALLBACK( h8s_tmr_callback)
651{
245652   h83xx_state *h8 = (h83xx_state *)ptr;
246   H8S2XXX_TPU *tpu = &h8->tpu[param];
247   _logerror( 2, "h8s_tpu_callback (%d)\n", param);
248   switch (param)
653   h8s_tmr_x_callback( h8, param);
654}
655
656//////////////////
657// TIMER 16-BIT //
658//////////////////
659
660void h8s_tpu_init(h83xx_state *h8)
661{
662   const int tpu_max = (h8->device->type() == H8S2323) ? 6 : 3;
663   for (int i = 0; i < tpu_max; i++)
249664   {
250      case 1 :
665      h8->tpu[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tpu_callback), h8);
666      h8->tpu[i].timer->adjust(attotime::never, i);
667   }
668}
669
670void h8s_tpu_x_reset( h83xx_state *h8, int x)
671{
672   H8S2XXX_TPU *tpu = &h8->tpu[x];
673   tpu->cycles_per_tick = 0;
674   tpu->timer->reset();
675}
676
677UINT16 h8s_tpu_x_calculate_tcnt( h83xx_state *h8, int x)
678{
679   H8S2XXX_TPU *tpu = &h8->tpu[x];
680   UINT16 tcnt;
681   tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
682   if ((tpu->timer->enabled()) && (tpu->cycles_per_tick > 0))
683   {
684      tcnt = (UINT16)(tcnt + ((device_icount_min_timer_icount1( h8, tpu->timer)) / tpu->cycles_per_tick));
685   }
686   return tcnt;
687}
688
689UINT64 h8s_tpu_x_calculate_ticks( h83xx_state *h8, int x, UINT16 tcnt)
690{
691   UINT64 cycles1, cycles2;
692   UINT16 tgr;
693   // overflow
694   if (tcnt == 0) cycles1 = 0xFFFF + 1; else cycles1 = (UINT16)(0xFFFF - tcnt + 1);
695   // tgra
696   tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRA_H[x]);
697   if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
698   if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
699   // tgrb
700   tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRB_H[x]);
701   if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
702   if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
703   // tgrc
704   if ((x == 0) || (x == 3))
705   {
706      tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRC_H[x]);
707      if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
708      if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
709   }
710   // tgrd
711   if ((x == 0) || (x == 3))
712   {
713      tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRD_H[x]);
714      if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
715      if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
716   }
717   // done
718   return cycles1;
719}
720
721void h8s_pin_write( h83xx_state *h8, int pin, int data)
722{
723   switch (pin)
724   {
725      case PIN_TIOCB1 :
251726      {
252         UINT32 hz, cycles;
253         int tiocb1, vecnum;
254         // get info
255         vecnum = tpu->item[tpu->tgrcur].irq;
256         tiocb1 = tpu->item[tpu->tgrcur].out;
257         if (tpu->tgrcur >= tpu->tgrmax) tpu->tgrcur = 0;
258         cycles = tpu->item[tpu->tgrcur+1].tgr - tpu->item[tpu->tgrcur+0].tgr;
259         tpu->tgrcur++;
260         // adjust timer
261         hz = h8->device->unscaled_clock() / (tpu->prescaler * cycles);
262         tpu->timer->adjust(attotime::from_hz(hz), 1);
263         // output
264         set_p1dr_tiocb1(h8, tiocb1);
265         // interrupt
266         if (vecnum != 0) h8s2xxx_interrupt_request(h8, vecnum);
727         UINT8 p1dr;
728         p1dr = h8->per_regs[H8S_IO_P1DR];
729         if (data) p1dr |= H8S_P1_TIOCB1; else p1dr &= ~H8S_P1_TIOCB1;
730         h8->io->write_byte(H8S_IO_ADDR(H8S_IO_P1DR), p1dr);
731         h8->per_regs[H8S_IO_P1DR] = p1dr;
267732      }
268733      break;
269734   }
270735}
271736
272void h8s_tpu_init(h83xx_state *h8)
737int h8s_pin_read( h83xx_state *h8, int pin)
273738{
274   int i;
275   for (i=0;i<3;i++)
739   switch (pin)
276740   {
277      h8->tpu[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tpu_callback), h8);
278      h8->tpu[i].timer->adjust(attotime::never, i);
741      case PIN_TIOCB1 : return (h8->per_regs[H8S_IO_P1DR] & H8S_P1_TIOCB1) ? 1 : 0;
279742   }
743   return 0;
280744}
281745
282void h8s_tpu_start(h83xx_state *h8, int num)
746void h8s_tioc_pin_update( h83xx_state *h8, int io, int pin)
283747{
284   UINT8 ttcr, tior, tier, tsr;
285   UINT16 tgra, tgrb; //, tcnt;
286   UINT32 hz, i, cycles;
287   H8S2XXX_TPU *tpu = &h8->tpu[num];
288   switch (num)
748   switch (io)
289749   {
290      case 1 :
750      case 1 : h8s_pin_write( h8, pin, 0); break;
751      case 2 : h8s_pin_write( h8, pin, 1); break;
752      case 3 : h8s_pin_write( h8, pin, h8s_pin_read( h8, pin) ? 0 : 1); break;
753      case 5 : h8s_pin_write( h8, pin, 0); break;
754      case 6 : h8s_pin_write( h8, pin, 1); break;
755      case 7 : h8s_pin_write( h8, pin, h8s_pin_read( h8, pin) ? 0 : 1); break;
756   }
757}
758
759void h8s_tioc_pin_reset( h83xx_state *h8, int io, int pin)
760{
761   switch (io)
762   {
763      case 1 : h8s_pin_write( h8, pin, 0); break;
764      case 2 : h8s_pin_write( h8, pin, 0); break;
765      case 3 : h8s_pin_write( h8, pin, 0); break;
766      case 5 : h8s_pin_write( h8, pin, 1); break;
767      case 6 : h8s_pin_write( h8, pin, 1); break;
768      case 7 : h8s_pin_write( h8, pin, 1); break;
769   }
770}
771
772void h8s_tpu_x_tioc_reset( h83xx_state *h8, int x)
773{
774   UINT8 tior;
775   int cclr;
776   // ...
777   cclr = (h8->per_regs[H8S_IO_TTCR[x]] >> 5) & TCR_CCLR_AND[x];
778   // tioca & tiocb
779   tior = h8->per_regs[H8S_IO_TIOR_H[x]];
780   if (cclr != 1) h8s_tioc_pin_reset( h8, (tior >> 0) & 0x0F, PIN_TIOCA[x]);
781   if (cclr != 2) h8s_tioc_pin_reset( h8, (tior >> 4) & 0x0F, PIN_TIOCB[x]);
782   // tiocc & tiocd
783   if ((x == 0) || (x == 3))
784   {
785      tior = h8->per_regs[H8S_IO_TIOR_L[x]];
786      if (cclr != 5) h8s_tioc_pin_reset( h8, (tior >> 0) & 0x0F, PIN_TIOCC[x]);
787      if (cclr != 6) h8s_tioc_pin_reset( h8, (tior >> 4) & 0x0F, PIN_TIOCD[x]);
788   }
789}
790
791void h8s_tpu_x_overflow( h83xx_state *h8, int x)
792{
793   if (x == 2)
794   {
795      UINT8 tcr = h8->per_regs[H8S_IO_TTCR[1]];
796      if (((tcr >> 0) & 7) == 7)
291797      {
292         ttcr = h8->per_regs[H8S_IO_TTCR1];
293         tior = h8->per_regs[H8S_IO_TIOR1];
294         tier = h8->per_regs[H8S_IO_TIER1];
295         tsr  = h8->per_regs[H8S_IO_TSR1];
296         tgra = (h8->per_regs[H8S_IO_TGR1A+0] << 8) | (h8->per_regs[H8S_IO_TGR1A+1] << 0);
297         tgrb = (h8->per_regs[H8S_IO_TGR1B+0] << 8) | (h8->per_regs[H8S_IO_TGR1B+1] << 0);
298//         tcnt = (h8->per_regs[H8S_IO_TTCNT1+0] << 8) | (h8->per_regs[H8S_IO_TTCNT1+1] << 0);
798         h8s_tpu_x_tick( h8, 1);
799      }
800   }
801   if (x == 5)
802   {
803      UINT8 tcr = h8->per_regs[H8S_IO_TTCR[4]];
804      if (((tcr >> 0) & 7) == 7)
805      {
806         h8s_tpu_x_tick( h8, 4);
807      }
808   }
809}
299810
300         //printf( "TTCR %02X TIOR %02X TIER %02X TSR %02X TGRA %04X TGRB %04X TCNT %04X\n", ttcr, tior, tier, tsr, tgra, tgrb, tcnt);
301
302         if ((ttcr != 0x21) || (tior != 0x56) || ((tier != 0x00) && (tier != 0x01)) || (tsr != 0x00))
811UINT16 h8s_tpu_x_tcnt_change( h83xx_state *h8, int x, UINT16 tcnt)
812{
813   UINT16 tgr;
814   UINT8 tcr, tier, tior;
815   int clear = 0;
816   // ...
817   verboselog( h8->device->machine(), 9, "TPU %d - tpu_x_tcnt_change (%04X)\n", x, tcnt);
818   // ...
819   tcr = h8->per_regs[H8S_IO_TTCR[x]];
820   tier = h8->per_regs[H8S_IO_TIER[x]];
821   // overflow
822   if (tcnt == 0)
823   {
824      verboselog( h8->device->machine(), 9, "TPU %d - overflow match\n", x);
825      h8->per_regs[H8S_IO_TSR[x]] |= TSR_TCFV;
826      if (tier & TIER_TCIEV)
827      {
828         h8s2xxx_interrupt_request( h8, H8S_INT_TCIV[x]);
829      }
830      h8s_tpu_x_overflow( h8, x);
831   }
832   // tgra
833   tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRA_H[x]);
834   if (tcnt == tgr)
835   {
836      verboselog( h8->device->machine(), 9, "TPU %d - tgra match\n", x);
837      h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFA;
838      if (tier & TIER_TGIEA)
839      {
840         h8s2xxx_interrupt_request( h8, H8S_INT_TGIA[x]);
841      }
842      if (((tcr >> 5) & TCR_CCLR_AND[x]) == 1) // CCLR 1 -> "TCNT cleared by TGRA compare match/input capture"
843      {
844         clear = 1;
845      }
846      else
847      {
848         tior = h8->per_regs[H8S_IO_TIOR_H[x]];
849         h8s_tioc_pin_update( h8, (tior >> 0) & 0x0F, PIN_TIOCA[x]);
850      }
851   }
852   // tgrb
853   tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRB_H[x]);
854   if (tcnt == tgr)
855   {
856      verboselog( h8->device->machine(), 9, "TPU %d - tgrb match\n", x);
857      h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFB;
858      if (tier & TIER_TGIEB)
859      {
860         h8s2xxx_interrupt_request( h8, H8S_INT_TGIB[x]);
861      }
862      if (((tcr >> 5) & TCR_CCLR_AND[x]) == 2) // CCLR 2 -> "TCNT cleared by TGRB compare match/input capture"
863      {
864         clear = 1;
865      }
866      else
867      {
868         tior = h8->per_regs[H8S_IO_TIOR_H[x]];
869         h8s_tioc_pin_update( h8, (tior >> 4) & 0x0F, PIN_TIOCB[x]);
870      }
871   }
872   // tgrc
873   if ((x == 0) || (x == 3))
874   {
875      tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRC_H[x]);
876      if (tcnt == tgr)
877      {
878         verboselog( h8->device->machine(), 9, "TPU %d - tgrc match\n", x);
879         h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFC;
880         if (tier & TIER_TGIEC)
303881         {
304            fatalerror( "TPU: not supported\n");
882            h8s2xxx_interrupt_request( h8, H8S_INT_TGIC[x]);
305883         }
306
307         // start
308         i = 0;
309         tpu->item[i].tgr = 0;
310         tpu->item[i].out = (i + 1) & 1;
311         tpu->item[i].irq = 0;
312         // TGRB
313         if (tgrb < tgra) i = 1; else i = 2;
314         tpu->item[i].tgr = tgrb;
315         tpu->item[i].out = (i + 1) & 1;
316         if (tier & 2) tpu->item[i].irq = H8S_INT_TGI1B; else tpu->item[i].irq = 0;
317         // TGRA
318         if (tgrb < tgra) i = 2; else i = 1;
319         tpu->item[i].tgr = tgra;
320         tpu->item[i].out = (i + 1) & 1;
321         if (tier & 1) tpu->item[i].irq = H8S_INT_TGI1A; else tpu->item[i].irq = 0;
322         // ...
323         tpu->tgrcur = 0;
324         tpu->tgrmax = 2;
325         tpu->prescaler = TPSC[ttcr & 7];
326         cycles = tpu->item[1].tgr - tpu->item[0].tgr;
327         // ajust timer
328         hz = h8->device->unscaled_clock() / (tpu->prescaler * cycles);
329         tpu->timer->adjust(attotime::from_hz(hz), num);
330         // output
331         set_p1dr_tiocb1(h8, tpu->item[0].out);
332         //
333         tpu->tgrcur++;
884         if (((tcr >> 5) & TCR_CCLR_AND[x]) == 5) // CCLR 5 -> "TCNT cleared by TGRC compare match/input capture"
885         {
886            clear = 1;
887         }
888         else
889         {
890            tior = h8->per_regs[H8S_IO_TIOR_L[x]];
891            h8s_tioc_pin_update( h8, (tior >> 0) & 0x0F, PIN_TIOCC[x]);
892         }
334893      }
335894   }
895   // tgrd
896   if ((x == 0) || (x == 3))
897   {
898      tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRD_H[x]);
899      if (tcnt == tgr)
900      {
901         verboselog( h8->device->machine(), 9, "TPU %d - tgrd match\n", x);
902         h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFD;
903         if (tier & TIER_TGIED)
904         {
905            h8s2xxx_interrupt_request( h8, H8S_INT_TGID[x]);
906         }
907         if (((tcr >> 5) & TCR_CCLR_AND[x]) == 6) // CCLR 6 -> "TCNT cleared by TGRD compare match/input capture"
908         {
909            clear = 1;
910         }
911         else
912         {
913            tior = h8->per_regs[H8S_IO_TIOR_L[x]];
914            h8s_tioc_pin_update( h8, (tior >> 4) & 0x0F, PIN_TIOCD[x]);
915         }
916      }
917   }
918   // ...
919   if (clear)
920   {
921      verboselog( h8->device->machine(), 9, "TPU %d - tcnt clear\n", x);
922      h8s_tpu_x_tioc_reset( h8, x);
923      tcnt = 0;
924   }
925   // ...
926   return tcnt;
336927}
337928
338void h8s_tpu_stop(h83xx_state *h8, int num)
929void h8s_tpu_x_callback( h83xx_state *h8, int x)
339930{
340   h8->tpu[num].timer->adjust(attotime::never, num);
931   H8S2XXX_TPU *tpu = &h8->tpu[x];
932   UINT64 cycles;
933   UINT16 tcnt;
934   // calculate tcnt
935   tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
936   tcnt = (UINT16)(tcnt + tpu->timer_cycles / tpu->cycles_per_tick);
937   // ...
938   tcnt = h8s_tpu_x_tcnt_change( h8, x, tcnt);
939   // update tcnt
940   ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
941   // update timer
942   cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt) * tpu->cycles_per_tick;
943   tpu->timer_cycles = cycles;
944   tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
341945}
342946
343void h8s_tpu_resync(h83xx_state *h8, UINT8 data)
947void h8s_tpu_x_tick( h83xx_state *h8, int x)
344948{
345   if (data & 1) h8s_tpu_start(h8, 0); else h8s_tpu_stop(h8, 0);
346   if (data & 2) h8s_tpu_start(h8, 1); else h8s_tpu_stop(h8, 1);
347   if (data & 4) h8s_tpu_start(h8, 2); else h8s_tpu_stop(h8, 2);
949   UINT16 tcnt;
950   // calculate tcnt
951   tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
952   tcnt = (UINT16)(tcnt + 1);
953   // ...
954   tcnt = h8s_tpu_x_tcnt_change( h8, x, tcnt);
955   // update tcnt
956   ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
348957}
349958
959int h8s_tpu_x_get_div( h83xx_state *h8, int x)
960{
961   UINT8 tcr;
962   int tpsc;
963   const int *TPU_X_DIV[] = { TPU_0_DIV, TPU_1_DIV, TPU_2_DIV, TPU_3_DIV, TPU_4_DIV, TPU_5_DIV };
964   tcr = h8->per_regs[H8S_IO_TTCR[x]];
965   tpsc = (tcr >> 0) & 7;
966   return TPU_X_DIV[x][tpsc];
967}
968
969void h8s_tpu_x_update( h83xx_state *h8, int x)
970{
971   H8S2XXX_TPU *tpu = &h8->tpu[x];
972   // ...
973   tpu->cycles_per_tick = h8s_tpu_x_get_div( h8, x);
974   // update timer
975   if (tpu->cycles_per_tick != 0)
976   {
977      UINT16 tcnt;
978      UINT64 cycles;
979      tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
980      cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt) * tpu->cycles_per_tick;
981      tpu->timer_cycles = cycles;
982      tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
983   }
984   else
985   {
986      tpu->timer->reset();
987   }
988}
989
990void h8s_tpu_x_start( h83xx_state *h8, int x)
991{
992   verboselog( h8->device->machine(), 5, "TPU %d - start\n", x);
993   // ...
994   h8s_tpu_x_update( h8, x);
995}
996
997void h8s_tpu_x_stop( h83xx_state *h8, int x)
998{
999   H8S2XXX_TPU *tpu = &h8->tpu[x];
1000   UINT16 tcnt;
1001   // ...
1002   verboselog( h8->device->machine(), 5, "TPU %d - stop\n", x);
1003   // calculate tcnt
1004   tcnt = h8s_tpu_x_calculate_tcnt( h8, x);
1005   // update tcnt
1006   ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
1007   // disable timer
1008   tpu->timer->reset();
1009}
1010
1011UINT16 h8s_tpu_x_read_tcnt( h83xx_state *h8, int x)
1012{
1013   return h8s_tpu_x_calculate_tcnt( h8, x);
1014}
1015
1016void h8s_tpu_x_write_tcnt( h83xx_state *h8, int x, UINT16 data)
1017{
1018   H8S2XXX_TPU *tpu = &h8->tpu[x];
1019   verboselog( h8->device->machine(), 9, "TPU %d - write tcnt (%04X)\n", x, data);
1020   ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], data);
1021   if (tpu->timer->enabled())
1022   {
1023      h8s_tpu_x_update( h8, x);
1024   }
1025}
1026
1027void h8s_tpu_x_write_tgr( h83xx_state *h8, int x)
1028{
1029   H8S2XXX_TPU *tpu = &h8->tpu[x];
1030   if ((tpu->timer->enabled()) && (tpu->cycles_per_tick != 0))
1031   {
1032      UINT16 tcnt1, tcnt2;
1033      UINT64 cycles;
1034      tcnt1 = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
1035      tcnt2 = (UINT16)(tcnt1 + ((device_icount_min_timer_icount1( h8, tpu->timer)) / tpu->cycles_per_tick));
1036      cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt2) * tpu->cycles_per_tick;
1037      cycles = cycles + (tcnt2 - tcnt1) * tpu->cycles_per_tick;
1038      tpu->timer_cycles = cycles;
1039      tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
1040   }
1041}
1042
1043void h8s_tpu_x_write_tgra( h83xx_state *h8, int x, UINT16 data)
1044{
1045   verboselog( h8->device->machine(), 9, "TPU %d - write tgra (%04X)\n", x, data);
1046   ptr_write_16( h8->per_regs + H8S_IO_TGRA_H[x], data);
1047   h8s_tpu_x_write_tgr( h8, x);
1048}
1049
1050void h8s_tpu_x_write_tgrb( h83xx_state *h8, int x, UINT16 data)
1051{
1052   verboselog( h8->device->machine(), 9, "TPU %d - write tgrb (%04X)\n", x, data);
1053   ptr_write_16( h8->per_regs + H8S_IO_TGRB_H[x], data);
1054   h8s_tpu_x_write_tgr( h8, x);
1055}
1056
1057void h8s_tpu_x_write_tgrc( h83xx_state *h8, int x, UINT16 data)
1058{
1059   verboselog( h8->device->machine(), 9, "TPU %d - write tgrc (%04X)\n", x, data);
1060   ptr_write_16( h8->per_regs + H8S_IO_TGRC_H[x], data);
1061   h8s_tpu_x_write_tgr( h8, x);
1062}
1063
1064void h8s_tpu_x_write_tgrd( h83xx_state *h8, int x, UINT16 data)
1065{
1066   verboselog( h8->device->machine(), 9, "TPU %d - write tgrd (%04X)\n", x, data);
1067   ptr_write_16( h8->per_regs + H8S_IO_TGRD_H[x], data);
1068   h8s_tpu_x_write_tgr( h8, x);
1069}
1070
1071void h8s_tpu_x_write_tior( h83xx_state *h8, int x)
1072{
1073   UINT8 tstr;
1074   tstr = h8->per_regs[H8S_IO_TSTR];
1075   if ((tstr & (1 << x)) == 0)
1076   {
1077      h8s_tpu_x_tioc_reset( h8, x);
1078   }
1079}
1080
1081void h8s_tpu_0_write_tior_h( h83xx_state *h8, UINT8 data)
1082{
1083   h8->per_regs[H8S_IO_TIOR0_H] = data;
1084   h8s_tpu_x_write_tior( h8, 0);
1085}
1086
1087void h8s_tpu_0_write_tior_l( h83xx_state *h8, UINT8 data)
1088{
1089   h8->per_regs[H8S_IO_TIOR0_L] = data;
1090   h8s_tpu_x_write_tior( h8, 0);
1091}
1092
1093void h8s_tpu_1_write_tior( h83xx_state *h8, UINT8 data)
1094{
1095   h8->per_regs[H8S_IO_TIOR1] = data;
1096   h8s_tpu_x_write_tior( h8, 1);
1097}
1098
1099void h8s_tpu_2_write_tior( h83xx_state *h8, UINT8 data)
1100{
1101   h8->per_regs[H8S_IO_TIOR2] = data;
1102   h8s_tpu_x_write_tior( h8, 2);
1103}
1104
1105void h8s_tpu_3_write_tior_h( h83xx_state *h8, UINT8 data)
1106{
1107   h8->per_regs[H8S_IO_TIOR3_H] = data;
1108   h8s_tpu_x_write_tior( h8, 3);
1109}
1110
1111void h8s_tpu_3_write_tior_l( h83xx_state *h8, UINT8 data)
1112{
1113   h8->per_regs[H8S_IO_TIOR3_L] = data;
1114   h8s_tpu_x_write_tior( h8, 3);
1115}
1116
1117void h8s_tpu_4_write_tior( h83xx_state *h8, UINT8 data)
1118{
1119   h8->per_regs[H8S_IO_TIOR4] = data;
1120   h8s_tpu_x_write_tior( h8, 4);
1121}
1122
1123void h8s_tpu_5_write_tior( h83xx_state *h8, UINT8 data)
1124{
1125   h8->per_regs[H8S_IO_TIOR5] = data;
1126   h8s_tpu_x_write_tior( h8, 5);
1127}
1128
1129void h8s_tpu_write_tstr( h83xx_state *h8, UINT8 data)
1130{
1131   const int tpu_max = (h8->device->type() == H8S2323) ? 6 : 3;
1132   UINT8 old_data = h8->per_regs[H8S_IO_TSTR];
1133   int i;
1134   h8->per_regs[H8S_IO_TSTR] = data;
1135   for (i = 0; i < tpu_max; i++)
1136   {
1137      if ((data & (1 << i)) != (old_data & (1 << i)))
1138      {
1139         if ((data & (1 << i)) != 0)
1140         {
1141            h8s_tpu_x_start( h8, i);
1142         }
1143         else
1144         {
1145            h8s_tpu_x_stop( h8, i);
1146         }
1147      }
1148   }
1149}
1150
1151static TIMER_CALLBACK( h8s_tpu_callback)
1152{
1153   h83xx_state *h8 = (h83xx_state *)ptr;
1154   h8s_tpu_x_callback( h8, param);
1155}
1156
3501157/////////////////////////////////
3511158// SERIAL CONTROLLER INTERFACE //
3521159/////////////////////////////////
r12440r12441
3941201   int i;
3951202   for (i=0;i<3;i++)
3961203   {
397      #ifdef ENABLE_SCI_TIMER
3981204      h8->sci[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_sci_callback), h8);
3991205      h8->sci[i].timer->adjust(attotime::never, i);
400      #endif
4011206   }
4021207}
4031208
404#ifdef ENABLE_SCI_TIMER
4051209static TIMER_CALLBACK(h8s_sci_callback)
4061210{
407//   h83xx_state *h8 = (h83xx_state *)ptr;
408   _logerror( 2, "h8s_sci_callback (%d)\n", param);
1211   h83xx_state *h8 = (h83xx_state *)ptr;
1212   verboselog( h8->device->machine(), 7, "h8s_sci_callback (%d)\n", param);
4091213}
410#endif
4111214
4121215void h8s_sci_start(h83xx_state *h8, int num)
4131216{
414   #ifdef ENABLE_SCI_TIMER
1217   #if 0
4151218   h8->sci[num].timer->adjust(h8->sci[num].bitrate, num);
4161219   #endif
4171220}
4181221
4191222void h8s_sci_stop(h83xx_state *h8, int num)
4201223{
421   #ifdef ENABLE_SCI_TIMER
4221224   h8->sci[num].timer->adjust(attotime::never, num);
423   #endif
4241225}
4251226
4261227void h8s_sci_execute(h83xx_state *h8, int num)
r12440r12441
4281229   UINT8 scr, tdr, ssr, rdr, tsr, rsr, pdr, port;
4291230   int i;
4301231   const H8S_SCI_ENTRY *info = h8s_sci_entry( num);
431//   logerror( "h8s_sci_execute(%d)\n", num);
1232   verboselog( h8->device->machine(), 5, "h8s_sci_execute(%d)\n", num);
4321233   // load regs
4331234   scr = h8->per_regs[info->reg_scr];
4341235   tdr = h8->per_regs[info->reg_tdr];
r12440r12441
4911292   brr = h8->per_regs[info->reg_brr];
4921293   scr = h8->per_regs[info->reg_scr];
4931294   smr = h8->per_regs[info->reg_smr];
494   _logerror( 2, "BRR %02X SCR %02X SMR %02X\n", brr, scr, smr);
1295   verboselog( h8->device->machine(), 2, "BRR %02X SCR %02X SMR %02X\n", brr, scr, smr);
4951296   // calculate bitrate
4961297   cke = (scr >> 0) & 3;
4971298   mode = (smr >> 7) & 1;
4981299   bitrate = (UINT32)((h8->device->unscaled_clock() / (brr + 1)) / (SMR_MODE[mode] * SCR_CKE[cke]));
499   _logerror( 2, "SCI%d bitrate %d\n", num, bitrate);
1300   verboselog( h8->device->machine(), 2, "SCI%d bitrate %d\n", num, bitrate);
5001301   // store bitrate
501   #ifdef ENABLE_SCI_TIMER
5021302   h8->sci[num].bitrate = bitrate;
503   #endif
5041303}
5051304
5061305////////////////////////////
5071306// INTERNAL I/O REGISTERS //
5081307////////////////////////////
5091308
510void h8s2xxx_per_regs_w_byte(h83xx_state *h8, int offset, UINT8 data)
1309void h8s_onchip_reg_write_8(h83xx_state *h8, int offset, UINT8 data)
5111310{
512   h8->per_regs[offset] = data;
1311   verboselog( h8->device->machine(), 9, "%08X | %08X <- %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
5131312   switch (offset)
5141313   {
5151314      // SCI 0
516      case H8S_IO_SSR0  : if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 0); break;
517      case H8S_IO_SCR0  : if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8, h8s_sci_entry(0)->int_tx); break;
518      case H8S_IO_BRR0  : h8s_sci_rate(h8, 0); h8s_sci_start(h8, 0); break;
1315      case H8S_IO_SSR0  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 0); break;
1316      case H8S_IO_SCR0  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8, h8s_sci_entry(0)->int_tx); break;
1317      case H8S_IO_BRR0  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 0); h8s_sci_start(h8, 0); break;
5191318      // SCI 1
520      case H8S_IO_SSR1  : if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 1); break;
521      case H8S_IO_SCR1  :   if (data & H8S_SCR_RIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(1)->int_rx); break;
522      case H8S_IO_BRR1  : h8s_sci_rate(h8, 1); h8s_sci_start(h8, 1); break;
1319      case H8S_IO_SSR1  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 1); break;
1320      case H8S_IO_SCR1  :   h8->per_regs[offset] = data; if (data & H8S_SCR_RIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(1)->int_rx); break;
1321      case H8S_IO_BRR1  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 1); h8s_sci_start(h8, 1); break;
5231322      // SCI 2
524      case H8S_IO_SSR2  : if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 2); break;
525      case H8S_IO_SCR2  : if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(2)->int_tx); break;
526      case H8S_IO_BRR2  : h8s_sci_rate(h8, 2); h8s_sci_start(h8, 2); break;
527      // TMR 0 (8-bit)
528      case H8S_IO_TCR0  : h8s_tmr_resync(h8, 0); break;
529      case H8S_IO_TCNT0 : h8s_tmr_resync(h8, 0); break;
1323      case H8S_IO_SSR2  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 2); break;
1324      case H8S_IO_SCR2  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(2)->int_tx); break;
1325      case H8S_IO_BRR2  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 2); h8s_sci_start(h8, 2); break;
1326      // TMR 0
1327      case H8S_IO_TCR0   : h8s_tmr_x_write_tcr( h8, 0, data); break;
1328      case H8S_IO_TCNT0  : h8s_tmr_x_write_tcnt( h8, 0, data); break;
1329      case H8S_IO_TCORA0 : h8s_tmr_x_write_tcora( h8, 0, data); break;
1330      case H8S_IO_TCORB0 : h8s_tmr_x_write_tcorb( h8, 0, data); break;
5301331      // TMR 1
531      case H8S_IO_TCR1  : h8s_tmr_resync(h8, 1); break;
532      case H8S_IO_TCNT1 : h8s_tmr_resync(h8, 1); break;
1332      case H8S_IO_TCR1   : h8s_tmr_x_write_tcr( h8, 1, data); break;
1333      case H8S_IO_TCNT1  : h8s_tmr_x_write_tcnt( h8, 1, data); break;
1334      case H8S_IO_TCORA1 : h8s_tmr_x_write_tcora( h8, 1, data); break;
1335      case H8S_IO_TCORB1 : h8s_tmr_x_write_tcorb( h8, 1, data); break;
5331336      // ports
534      case H8S_IO_P3DR  : h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
535      case H8S_IO_PFDR  : h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
536      case H8S_IO_PFDDR : h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
1337      case H8S_IO_P1DR : case H8S_IO_P2DR : case H8S_IO_P3DR : case H8S_IO_P4DR : case H8S_IO_P5DR :
1338      case H8S_IO_PADR : case H8S_IO_PBDR : case H8S_IO_PCDR : case H8S_IO_PDDR : case H8S_IO_PEDR :
1339      case H8S_IO_PFDR : h8->per_regs[offset] = data; h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
1340      // ...
1341      case H8S_IO_PFDDR : h8->per_regs[offset] = data; h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
5371342      // TPU
538      case H8S_IO_TSTR  : h8s_tpu_resync(h8, data); break;
1343      case H8S_IO_TSTR  : h8s_tpu_write_tstr( h8, data); break;
1344      // DMA
1345      case H8S_IO_DMABCRL :
1346      {
1347         h8->per_regs[offset] = data;
1348         if (h8->device->type() == H8S2323)
1349         {
1350            if ((data & 0x40) && (data & 0x80))
1351            {
1352               UINT32 i, dma_src, dma_dst;
1353               UINT16 dma_cnt, dma_con;
1354               int sz;
1355               dma_src = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1AH));
1356               dma_dst = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1BH));
1357               dma_cnt = h8->program->read_word( H8S_IO_ADDR(H8S_IO_ETCR1A));
1358               dma_con = h8->program->read_word( H8S_IO_ADDR(H8S_IO_DMACR1A));
1359               sz = (dma_con & 0x8000) ? 2 : 1;
1360               for (i=0;i<dma_cnt;i++)
1361               {
1362                  if (dma_con & 0x8000) h8->program->write_word( dma_dst, h8->program->read_word( dma_src)); else h8->program->write_byte( dma_dst, h8->program->read_byte( dma_src));
1363                  if (dma_con & 0x2000) { if (dma_con & 0x4000) dma_src -= sz; else dma_src += sz; }
1364                  if (dma_con & 0x0020) { if (dma_con & 0x0040) dma_dst -= sz; else dma_dst += sz; }
1365               }
1366               h8->per_regs[H8S_IO_DMABCRL] &= ~0x40;
1367            }
1368         }
1369      }
1370      break;
1371      // ...
1372      default :
1373      {
1374         h8->per_regs[offset] = data;
1375      }
1376      break;
5391377   }
5401378}
5411379
542UINT8 h8s2xxx_per_regs_r_byte(h83xx_state *h8, int offset)
1380void h8s_onchip_reg_write_16(h83xx_state *h8, int offset, UINT16 data)
5431381{
1382   verboselog( h8->device->machine(), 9, "%08X | %08X <- %04X\n", h8->ppc, H8S_IO_ADDR(offset), data);
1383   switch (offset)
1384   {
1385      // TPU 0
1386      case H8S_IO_TCNT0_H : h8s_tpu_x_write_tcnt( h8, 0, data); break;
1387      case H8S_IO_TGR0A_H : h8s_tpu_x_write_tgra( h8, 0, data); break;
1388      case H8S_IO_TGR0B_H : h8s_tpu_x_write_tgrb( h8, 0, data); break;
1389      case H8S_IO_TGR0C_H : h8s_tpu_x_write_tgrc( h8, 0, data); break;
1390      case H8S_IO_TGR0D_H : h8s_tpu_x_write_tgrd( h8, 0, data); break;
1391      // TPU 1
1392      case H8S_IO_TCNT1_H : h8s_tpu_x_write_tcnt( h8, 1, data); break;
1393      case H8S_IO_TGR1A_H : h8s_tpu_x_write_tgra( h8, 1, data); break;
1394      case H8S_IO_TGR1B_H : h8s_tpu_x_write_tgrb( h8, 1, data); break;
1395      // TPU 2
1396      case H8S_IO_TCNT2_H : h8s_tpu_x_write_tcnt( h8, 2, data); break;
1397      case H8S_IO_TGR2A_H : h8s_tpu_x_write_tgra( h8, 2, data); break;
1398      case H8S_IO_TGR2B_H : h8s_tpu_x_write_tgrb( h8, 2, data); break;
1399      // TPU 3
1400      case H8S_IO_TCNT3_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tcnt( h8, 3, data); break;
1401      case H8S_IO_TGR3A_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgra( h8, 3, data); break;
1402      case H8S_IO_TGR3B_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgrb( h8, 3, data); break;
1403      case H8S_IO_TGR3C_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgrc( h8, 3, data); break;
1404      case H8S_IO_TGR3D_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgrd( h8, 3, data); break;
1405      // TPU 4
1406      case H8S_IO_TCNT4_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tcnt( h8, 4, data); break;
1407      case H8S_IO_TGR4A_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgra( h8, 4, data); break;
1408      case H8S_IO_TGR4B_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgrb( h8, 4, data); break;
1409      // TPU 5
1410      case H8S_IO_TCNT5_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tcnt( h8, 5, data); break;
1411      case H8S_IO_TGR5A_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgra( h8, 5, data); break;
1412      case H8S_IO_TGR5B_H : if (h8->device->type() == H8S2323) h8s_tpu_x_write_tgrb( h8, 5, data); break;
1413      // ...
1414      default :
1415      {
1416         h8s_onchip_reg_write_8( h8, offset + 0, (data >> 8) & 0xFF);
1417         h8s_onchip_reg_write_8( h8, offset + 1, (data >> 0) & 0xFF);
1418      }
1419      break;
1420   }
1421}
1422
1423UINT8 h8s_onchip_reg_read_8(h83xx_state *h8, int offset)
1424{
5441425   UINT8 data;
5451426   switch (offset)
5461427   {
r12440r12441
5571438      }
5581439      break;
5591440      // ports
560      case H8S_IO_PORT1 : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
1441      case H8S_IO_PORT1 : case H8S_IO_PORT2 : case H8S_IO_PORT3 : case H8S_IO_PORT4 : case H8S_IO_PORT5 :
1442      case H8S_IO_PORTA : case H8S_IO_PORTB : case H8S_IO_PORTC : case H8S_IO_PORTD : case H8S_IO_PORTE :
5611443      case H8S_IO_PORTF : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
1444      // ...
5621445      case H8S_IO_P3DR  : data = 0; break; // todo: without this cybiko hangs
1446      // TMR 0
1447      case H8S_IO_TCNT0 : data = h8s_tmr_x_read_tcnt( h8, 0); break;
1448      // TMR 1
1449      case H8S_IO_TCNT1 : data = h8s_tmr_x_read_tcnt( h8, 1); break;
1450      // ...
5631451      // default
5641452      default : data = h8->per_regs[offset]; break;
5651453   }
1454   verboselog( h8->device->machine(), 9, "%08X | %08X -> %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
5661455   return data;
5671456}
5681457
569WRITE8_HANDLER( h8s2241_per_regs_w_byte )
1458UINT16 h8s_onchip_reg_read_16(h83xx_state *h8, int offset)
5701459{
571   h83xx_state *h8 = get_safe_token(&space->device());
572   _logerror( 3, "[%08X] h8s2241_per_regs_w_byte (%08X/%02X)\n", h8->pc, H8S_IO_ADDR( offset), data);
573   h8s2xxx_per_regs_w_byte(h8, offset, data);
1460   UINT16 data = 0;
1461   switch (offset)
1462   {
1463      case H8S_IO_TCNT0_H : data = h8s_tpu_x_read_tcnt( h8, 0); break;
1464      case H8S_IO_TCNT1_H : data = h8s_tpu_x_read_tcnt( h8, 1); break;
1465      case H8S_IO_TCNT2_H : data = h8s_tpu_x_read_tcnt( h8, 2); break;
1466      case H8S_IO_TCNT3_H : if (h8->device->type() == H8S2323) data = h8s_tpu_x_read_tcnt( h8, 3); break;
1467      case H8S_IO_TCNT4_H : if (h8->device->type() == H8S2323) data = h8s_tpu_x_read_tcnt( h8, 4); break;
1468      case H8S_IO_TCNT5_H : if (h8->device->type() == H8S2323) data = h8s_tpu_x_read_tcnt( h8, 5); break;
1469      default :
1470      {
1471         UINT8 b[2];
1472         b[0] = h8s_onchip_reg_read_8( h8, offset + 0);
1473         b[1] = h8s_onchip_reg_read_8( h8, offset + 1);
1474         data = (b[0] << 8) | (b[1] << 0);
1475      }
1476      break;
1477   }
1478   verboselog( h8->device->machine(), 9, "%08X | %08X -> %04X\n", h8->ppc, H8S_IO_ADDR(offset), data);
1479   return data;
5741480}
5751481
576READ8_HANDLER( h8s2241_per_regs_r_byte )
1482void h8s2241_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
5771483{
578   h83xx_state *h8 = get_safe_token(&space->device());
579   UINT8 data = 0;
580   _logerror( 3, "[%08X] h8s2241_per_regs_r_byte (%08X)\n", h8->pc, H8S_IO_ADDR( offset));
581   data = h8s2xxx_per_regs_r_byte(h8, offset);
582   _logerror( 3, "%02X\n", data);
583   return data;
1484   h8s_onchip_reg_write_8(h8, offset, data);
5841485}
5851486
586WRITE8_HANDLER( h8s2246_per_regs_w_byte )
1487UINT8 h8s2241_per_regs_read_8(h83xx_state *h8, int offset)
5871488{
588   h83xx_state *h8 = get_safe_token(&space->device());
589   _logerror( 3, "[%08X] h8s2246_per_regs_w_byte (%08X/%02X)\n", h8->pc, H8S_IO_ADDR( offset), data);
590   h8s2xxx_per_regs_w_byte(h8, offset, data);
1489   return h8s_onchip_reg_read_8(h8, offset);
5911490}
5921491
593READ8_HANDLER( h8s2246_per_regs_r_byte )
1492void h8s2241_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
5941493{
595   h83xx_state *h8 = get_safe_token(&space->device());
596   UINT8 data=0;
597   _logerror( 3, "[%08X] h8s2246_per_regs_r_byte (%08X)\n", h8->pc, H8S_IO_ADDR( offset));
598   data = h8s2xxx_per_regs_r_byte(h8, offset);
599   _logerror( 3, "%02X\n", data);
600   return data;
1494   h8s_onchip_reg_write_16(h8, offset, data);
6011495}
6021496
603WRITE8_HANDLER( h8s2323_per_regs_w_byte )
1497UINT16 h8s2241_per_regs_read_16(h83xx_state *h8, int offset)
6041498{
605   h83xx_state *h8 = get_safe_token(&space->device());
606   _logerror( 3, "[%08X] h8s2323_per_regs_w_byte (%08X/%02X)\n", h8->pc, H8S_IO_ADDR( offset), data);
607   h8s2xxx_per_regs_w_byte(h8, offset, data);
608   switch (offset)
609   {
610      case H8S_IO_DMABCRL :
611      {
612         if ((data & 0x40) && (data & 0x80))
613         {
614            UINT32 i, dma_src, dma_dst;
615            UINT16 dma_cnt, dma_con;
616            int sz;
617            dma_src = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1AH));
618            dma_dst = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1BH));
619            dma_cnt = h8->program->read_word( H8S_IO_ADDR(H8S_IO_ETCR1A));
620            dma_con = h8->program->read_word( H8S_IO_ADDR(H8S_IO_DMACR1A));
621            sz = (dma_con & 0x8000) ? 2 : 1;
622            for (i=0;i<dma_cnt;i++)
623            {
624               if (dma_con & 0x8000) h8->program->write_word( dma_dst, h8->program->read_word( dma_src)); else h8->program->write_byte( dma_dst, h8->program->read_byte( dma_src));
625               if (dma_con & 0x2000) { if (dma_con & 0x4000) dma_src -= sz; else dma_src += sz; }
626               if (dma_con & 0x0020) { if (dma_con & 0x0040) dma_dst -= sz; else dma_dst += sz; }
627            }
628            h8->per_regs[H8S_IO_DMABCRL] &= ~0x40;
629         }
630      }
631      break;
632   }
1499   return h8s_onchip_reg_read_16(h8, offset);
6331500}
6341501
635READ8_HANDLER( h8s2323_per_regs_r_byte )
1502void h8s2246_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
6361503{
637   h83xx_state *h8 = get_safe_token(&space->device());
638   UINT8 data=0;
639   _logerror( 3, "[%08X] h8s2323_per_regs_r_byte (%08X)\n", h8->pc, H8S_IO_ADDR( offset));
640   switch (offset)
641   {
642      // timer hack for cybiko xtreme (increase timer value after low byte has been read)
643      case H8S_IO_TTCNT5 + 1 :
644      {
645         UINT16 tcnt = (h8->per_regs[H8S_IO_TTCNT5+0] << 8) + (h8->per_regs[H8S_IO_TTCNT5+1] << 0);
646         tcnt = tcnt + 1000;
647         data = h8->per_regs[offset];
648         h8->per_regs[H8S_IO_TTCNT5+0] = (tcnt >> 8) & 0xFF;
649         h8->per_regs[H8S_IO_TTCNT5+1] = (tcnt >> 0) & 0xFF;
650         break;
651      }
652      // skip "recharge the batteries"
653      case H8S_IO_PORTA : data = h8->per_regs[offset] | (1 << 6); break;
654      // default
655      default : data = h8s2xxx_per_regs_r_byte(h8, offset); break;
656   }
657   _logerror( 3, "%02X\n", data);
658   return data;
1504   h8s_onchip_reg_write_8(h8, offset, data);
6591505}
1506
1507UINT8 h8s2246_per_regs_read_8(h83xx_state *h8, int offset)
1508{
1509   return h8s_onchip_reg_read_8(h8, offset);
1510}
1511
1512void h8s2246_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
1513{
1514   h8s_onchip_reg_write_16(h8, offset, data);
1515}
1516
1517UINT16 h8s2246_per_regs_read_16(h83xx_state *h8, int offset)
1518{
1519   return h8s_onchip_reg_read_16(h8, offset);
1520}
1521
1522void h8s2323_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
1523{
1524   h8s_onchip_reg_write_8(h8, offset, data);
1525}
1526
1527UINT8 h8s2323_per_regs_read_8(h83xx_state *h8, int offset)
1528{
1529   return h8s_onchip_reg_read_8(h8, offset);
1530}
1531
1532void h8s2323_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
1533{
1534   h8s_onchip_reg_write_16(h8, offset, data);
1535}
1536
1537UINT16 h8s2323_per_regs_read_16(h83xx_state *h8, int offset)
1538{
1539   return h8s_onchip_reg_read_16(h8, offset);
1540}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team