Previous 199869 Revisions Next

r18790 Wednesday 31st October, 2012 at 00:10:27 UTC by Ryan Holtz
-avr8.c: Converted to modern CPU device. [MooglyGuy]
[src/emu/cpu/avr8]avr8.c avr8.h
[src/mess/drivers]craft.c uzebox.c

trunk/src/emu/cpu/avr8/avr8.c
r18789r18790
11/*
2    Atmel 8-bit AVR emulator
2    Atmel 8-bit AVR simulator
33
4    (Skeleton)
4    - Notes -
5      Cycle counts are generally considered to be 100% accurate per-instruction, does not support mid-instruction
6      interrupts although no software has been countered yet that requires it. Evidence of cycle accuracy is given
7      in the form of the demoscene 'wild' demo, Craft, by [lft], which uses an ATmega88 to write video out a 6-bit
8      RGB DAC pixel-by-pixel, synchronously with the frame timing. Intentionally modifying the timing of any of
9      the existing opcodes has been shown to wildly corrupt the video output in Craft, so one can assume that the
10      existing timing is 100% correct.
511
6    DONE:
7    - Disassembler
8    - [lft]'s "Craft" depends on on-chip device support now instead of opcodes (it requires unusually few in order to boot)
12     Unimplemented opcodes: CPSR, LD Z+, ST Z+, ST -Z/-Y/-X, ELPM, SPM, SPM Z+, EIJMP, SLEEP, BREAK, WDR, ICALL,
13                            EICALL, JMP, CALL, SBIW
914
10    TODO:
11    - Everything else
12      * Finish opcode implementation
13      * Add proper cycle timing
14      * Add Interrupts
15      * Add on-chip hardware (machine driver)
15   - Changelist -
16     30 Oct. 2012
17     - Added FMUL, FMULS, FMULSU opcodes [MooglyGuy]
18     - Fixed incorrect flag calculation in ROR opcode [MooglyGuy]
19     - Fixed incorrect bit testing in SBIC/SBIS opcodes [MooglyGuy]
1620
17    Written by MooglyGuy
21     25 Oct. 2012
22     - Added MULS, ANDI, STI Z+, LD -Z, LD -Y, LD -X, LD Y+q, LD Z+q, SWAP, ASR, ROR and SBIS opcodes [MooglyGuy]
23     - Corrected cycle counts for LD and ST opcodes [MooglyGuy]
24     - Moved opcycles init into inner while loop, fixes 2-cycle and 3-cycle opcodes effectively forcing
25       all subsequent 1-cycle opcodes to be 2 or 3 cycles [MooglyGuy]
26     - Fixed register behavior in MULSU, LD -Z, and LD -Y opcodes [MooglyGuy]
27
28     18 Oct. 2012
29     - Added OR, SBCI, ORI, ST Y+, ADIQ opcodes [MooglyGuy]
30     - Fixed COM, NEG, LSR opcodes [MooglyGuy]
31
1832*/
1933
2034#include "emu.h"
r18789r18790
4256#define verboselog(x,y,z,...)
4357#endif
4458
59//**************************************************************************
60//  ENUMS AND MACROS
61//**************************************************************************
62
4563enum
4664{
4765    AVR8_SREG_C = 0,
r18789r18790
89107
90108static const char avr8_reg_name[4] = { 'A', 'B', 'C', 'D' };
91109
92#define SREG_R(b) ((cpustate->status & (1 << (b))) >> (b))
93#define SREG_W(b,v) cpustate->status = (cpustate->status & ~(1 << (b))) | ((v) << (b))
110#define SREG_R(b) ((m_status & (1 << (b))) >> (b))
111#define SREG_W(b,v) m_status = (m_status & ~(1 << (b))) | ((v) << (b))
94112#define NOT(x) (1 - (x))
95113
96114// Opcode-Parsing Defines
r18789r18790
112130#define MULCONST2(op)   ((((op) >> 6) & 0x0002) | (((op) >> 3) & 0x0001))
113131
114132// Register Defines
115#define XREG            ((cpustate->r[27] << 8) | cpustate->r[26])
116#define YREG            ((cpustate->r[29] << 8) | cpustate->r[28])
117#define ZREG            ((cpustate->r[31] << 8) | cpustate->r[30])
118#define SPREG         ((cpustate->r[AVR8_REGIDX_SPH] << 8) | cpustate->r[AVR8_REGIDX_SPL])
133#define XREG            ((m_r[27] << 8) | m_r[26])
134#define YREG            ((m_r[29] << 8) | m_r[28])
135#define ZREG            ((m_r[31] << 8) | m_r[30])
136#define SPREG         ((m_r[AVR8_REGIDX_SPH] << 8) | m_r[AVR8_REGIDX_SPL])
119137
120138// I/O Defines
121#define AVR8_OCR1BH            (cpustate->r[AVR8_REGIDX_OCR1BH])
122#define AVR8_OCR1BL            (cpustate->r[AVR8_REGIDX_OCR1BL])
123#define AVR8_OCR1AH            (cpustate->r[AVR8_REGIDX_OCR1AH])
124#define AVR8_OCR1AL            (cpustate->r[AVR8_REGIDX_OCR1AL])
125#define AVR8_ICR1H            (cpustate->r[AVR8_REGIDX_ICR1H])
126#define AVR8_ICR1L            (cpustate->r[AVR8_REGIDX_ICR1L])
127#define AVR8_TCNT1H            (cpustate->r[AVR8_REGIDX_TCNT1H])
128#define AVR8_TCNT1L            (cpustate->r[AVR8_REGIDX_TCNT1L])
139#define AVR8_OCR1BH            (m_r[AVR8_REGIDX_OCR1BH])
140#define AVR8_OCR1BL            (m_r[AVR8_REGIDX_OCR1BL])
141#define AVR8_OCR1AH            (m_r[AVR8_REGIDX_OCR1AH])
142#define AVR8_OCR1AL            (m_r[AVR8_REGIDX_OCR1AL])
143#define AVR8_ICR1H            (m_r[AVR8_REGIDX_ICR1H])
144#define AVR8_ICR1L            (m_r[AVR8_REGIDX_ICR1L])
145#define AVR8_TCNT1H            (m_r[AVR8_REGIDX_TCNT1H])
146#define AVR8_TCNT1L            (m_r[AVR8_REGIDX_TCNT1L])
129147
130#define AVR8_TCCR1B               (cpustate->r[AVR8_REGIDX_TCCR1B])
148#define AVR8_TCCR1B               (m_r[AVR8_REGIDX_TCCR1B])
131149#define AVR8_TCCR1B_ICNC1_MASK      0x80
132150#define AVR8_TCCR1B_ICNC1_SHIFT      7
133151#define AVR8_TCCR1B_ICES1_MASK      0x40
r18789r18790
138156#define AVR8_TCCR1B_CS_SHIFT      0
139157#define AVR8_TIMER1_CLOCK_SELECT   (AVR8_TCCR1B & AVR8_TCCR1B_CS_MASK)
140158
141#define AVR8_TCCR1A               (cpustate->r[AVR8_REGIDX_TCCR1A])
159#define AVR8_TCCR1A               (m_r[AVR8_REGIDX_TCCR1A])
142160#define AVR8_TCCR1A_COM1A_MASK      0xc0
143161#define AVR8_TCCR1A_COM1A_SHIFT      6
144162#define AVR8_TCCR1A_COM1B_MASK      0x30
r18789r18790
149167#define AVR8_TCCR1A_COM1B         ((AVR8_TCCR1A & AVR8_TCCR1A_COM1B_MASK) >> AVR8_TCCR1A_COM1B_SHIFT)
150168#define AVR8_TCCR1A_WGM1_10         (AVR8_TCCR1A & AVR8_TCCR1A_WGM1_10_MASK)
151169
152#define AVR8_TIMSK1            (cpustate->r[AVR8_REGIDX_TIMSK1])
170#define AVR8_TIMSK1            (m_r[AVR8_REGIDX_TIMSK1])
153171#define AVR8_TIMSK1_ICIE1_MASK   0x20
154172#define AVR8_TIMSK1_OCIE1B_MASK   0x04
155173#define AVR8_TIMSK1_OCIE1A_MASK   0x02
r18789r18790
159177#define AVR8_TIMSK1_OCIE1A      ((AVR8_TIMSK1 & AVR8_TIMSK1_OCIE1A_MASK) >> 1)
160178#define AVR8_TIMSK1_TOIE1      (AVR8_TIMSK1 & AVR8_TIMSK1_TOIE1_MASK)
161179
162#define AVR8_TIFR1            (cpustate->r[AVR8_REGIDX_TIFR1])
180#define AVR8_TIFR1            (m_r[AVR8_REGIDX_TIFR1])
163181#define AVR8_TIFR1_ICF1_MASK   0x20
164182#define AVR8_TIFR1_ICF1_SHIFT   5
165183#define AVR8_TIFR1_OCF1B_MASK   0x04
r18789r18790
171189#define AVR8_TIFR1_MASK         (AVR8_TIFR1_ICF1_MASK | AVR8_TIFR1_TOV1_MASK | \
172190                         AVR8_TIFR1_OCF1B_MASK | AVR8_TIFR1_OCF1A_MASK)
173191
174#define AVR8_TCCR2B               (cpustate->r[AVR8_REGIDX_TCCR1B])
192#define AVR8_TCCR2B               (m_r[AVR8_REGIDX_TCCR1B])
175193#define AVR8_TCCR2B_FOC2A_MASK      0x80
176194#define AVR8_TCCR2B_FOC2A_SHIFT      7
177195#define AVR8_TCCR2B_FOC2B_MASK      0x40
r18789r18790
182200#define AVR8_TCCR2B_CS_SHIFT      0
183201#define AVR8_TIMER2_CLOCK_SELECT   (AVR8_TCCR2B & AVR8_TCCR2B_CS_MASK)
184202
185#define AVR8_TCCR2A               (cpustate->r[AVR8_REGIDX_TCCR1A])
203#define AVR8_TCCR2A               (m_r[AVR8_REGIDX_TCCR1A])
186204#define AVR8_TCCR2A_COM2A_MASK      0xc0
187205#define AVR8_TCCR2A_COM2A_SHIFT      6
188206#define AVR8_TCCR2A_COM2B_MASK      0x30
r18789r18790
193211#define AVR8_TCCR2A_COM2B         ((AVR8_TCCR2A & AVR8_TCCR2A_COM2B_MASK) >> AVR8_TCCR2A_COM2B_SHIFT)
194212#define AVR8_TCCR2A_WGM2_10         (AVR8_TCCR2A & AVR8_TCCR1A_WGM2_10_MASK)
195213
196#define AVR8_TIMSK2            (cpustate->r[AVR8_REGIDX_TIMSK2])
214#define AVR8_TIMSK2            (m_r[AVR8_REGIDX_TIMSK2])
197215#define AVR8_TIMSK2_OCIE2B_MASK   0x04
198216#define AVR8_TIMSK2_OCIE2A_MASK   0x02
199217#define AVR8_TIMSK2_TOIE2_MASK   0x01
r18789r18790
201219#define AVR8_TIMSK2_OCIE2A      ((AVR8_TIMSK2 & AVR8_TIMSK1_OCIE2A_MASK) >> 1)
202220#define AVR8_TIMSK2_TOIE2      (AVR8_TIMSK2 & AVR8_TIMSK1_TOIE2_MASK)
203221
204#define AVR8_TIFR2            (cpustate->r[AVR8_REGIDX_TIFR2])
222#define AVR8_TIFR2            (m_r[AVR8_REGIDX_TIFR2])
205223#define AVR8_TIFR2_OCF2B_MASK   0x04
206224#define AVR8_TIFR2_OCF2B_SHIFT   2
207225#define AVR8_TIFR2_OCF2A_MASK   0x02
r18789r18790
217235#define AVR8_WGM1            (((AVR8_TCCR1B & 0x18) >> 1) | (AVR8_TCCR1A & 0x03))
218236#define AVR8_TCNT1_DIR         (state->m_tcnt1_direction)
219237
220#define AVR8_OCR2B            cpustate->r[AVR8_REGIDX_OCR2B]
221#define AVR8_OCR2A            cpustate->r[AVR8_REGIDX_OCR2A]
222#define AVR8_TCNT2            cpustate->r[AVR8_REGIDX_TCNT2]
238#define AVR8_OCR2B            m_r[AVR8_REGIDX_OCR2B]
239#define AVR8_OCR2A            m_r[AVR8_REGIDX_OCR2A]
240#define AVR8_TCNT2            m_r[AVR8_REGIDX_TCNT2]
223241#define AVR8_WGM2            (((AVR8_TCCR2B & 0x08) >> 1) | (AVR8_TCCR2A & 0x03))
224242
225243#define AVR8_GTCCR_PSRASY_MASK   0x02
226244#define AVR8_GTCCR_PSRASY_SHIFT   1
227245
228INLINE avr8_state *get_safe_token(device_t *device)
229{
230    assert(device != NULL);
231    assert(device->type() == ATMEGA88 || device->type() == ATMEGA644);
232    return (avr8_state *)downcast<legacy_cpu_device *>(device)->token();
233}
246//**************************************************************************
247//  DEVICE INTERFACE
248//**************************************************************************
234249
235/*****************************************************************************/
236// Prototypes
250const device_type ATMEGA88 = &device_creator<atmega88_device>;
251const device_type ATMEGA644 = &device_creator<atmega644_device>;
237252
238// - Utility
239static void unimplemented_opcode(avr8_state *cpustate, UINT32 op);
253//-------------------------------------------------
254//  atmega8_device - constructor
255//-------------------------------------------------
240256
241// - Interrupts
242static void avr8_set_irq_line(avr8_state *cpustate, UINT16 vector, int state);
243static void avr8_update_interrupt_internal(avr8_state *cpustate, int source);
244//static void avr8_poll_interrupt(avr8_state *cpustate);
257avr8_device::avr8_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock, const device_type type, UINT32 addr_mask)
258   : cpu_device(mconfig, type, "AVR8", tag, owner, clock),
259     m_program_config("program", ENDIANNESS_LITTLE, 8, 22),
260     m_io_config("io", ENDIANNESS_LITTLE, 8, 16),
261      m_pc(0),
262      m_debugger_pc(0),
263      m_status(0),
264      m_timer0_top(0),
265      m_timer0_increment(1),
266      m_timer0_prescale(0),
267      m_timer0_prescale_count(0),
268      m_timer1_top(0),
269      m_timer1_increment(1),
270      m_timer1_prescale(0),
271      m_timer1_prescale_count(0),
272      m_timer2_top(0),
273      m_timer2_increment(1),
274      m_timer2_prescale(0),
275      m_timer2_prescale_count(0),
276      m_addr_mask(addr_mask),
277      m_interrupt_pending(false),
278      m_icount(0),
279      m_elapsed_cycles(0)
280{
281    // Allocate & setup
282}
245283
246// - Timers
247static void avr8_timer_tick(avr8_state *cpustate, int cycles);
248284
249// - Timer 0
250static void avr8_timer0_tick(avr8_state *cpustate);
285//-------------------------------------------------
286//  static_set_config - set the configuration
287//  structure
288//-------------------------------------------------
251289
252// - Timer 1
253static void avr8_timer1_tick(avr8_state *cpustate);
254static void avr8_change_timsk1(avr8_state *cpustate, UINT8 data);
255static void avr8_update_timer1_waveform_gen_mode(avr8_state *cpustate);
256static void avr8_changed_tccr1a(avr8_state *cpustate, UINT8 data);
257static void avr8_update_timer1_input_noise_canceler(avr8_state *cpustate);
258static void avr8_update_timer1_input_edge_select(avr8_state *cpustate);
259static void avr8_update_timer1_clock_source(avr8_state *cpustate);
260static void avr8_changed_tccr1b(avr8_state *cpustate, UINT8 data);
261static void avr8_update_ocr1(avr8_state *cpustate, UINT16 newval, UINT8 reg);
290void avr8_device::static_set_config(device_t &device, const avr8_config &config)
291{
292   avr8_device &avr8 = downcast<avr8_device &>(device);
293   static_cast<avr8_config &>(avr8) = config;
294}
262295
263// - Timer 2
264static void avr8_timer2_tick(avr8_state *cpustate);
265static void avr8_update_timer2_waveform_gen_mode(avr8_state *cpustate);
266static void avr8_changed_tccr2a(avr8_state *cpustate, UINT8 data);
267static void avr8_update_timer2_clock_source(avr8_state *cpustate);
268static void avr8_timer2_force_output_compare(avr8_state *cpustate, int reg);
269static void avr8_changed_tccr2b(avr8_state *cpustate, UINT8 data);
270static void avr8_update_ocr2(avr8_state *cpustate, UINT8 newval, UINT8 reg);
271296
272// - Register Handling
273static bool avr8_io_reg_write(avr8_state *cpustate, UINT16 offset, UINT8 data);
274static bool avr8_io_reg_read(avr8_state *cpustate, UINT16 offset, UINT8 *data);
297//-------------------------------------------------
298//  unimplemented_opcode - bail on unspuported
299//  instruction
300//-------------------------------------------------
275301
276/*****************************************************************************/
277// Utility Functions
278
279static void unimplemented_opcode(avr8_state *cpustate, UINT32 op)
302void avr8_device::unimplemented_opcode(UINT32 op)
280303{
281    fatalerror("AVR8: unknown opcode (%08x) at %08x\n", op, cpustate->pc);
304    fatalerror("AVR8: unknown opcode (%08x) at %08x\n", op, m_pc);
282305}
283306
284INLINE bool avr8_is_long_opcode(UINT16 op)
307
308//-------------------------------------------------
309//  is_long_opcode - returns true if opcode is 4
310//  bytes long
311//-------------------------------------------------
312
313inline bool avr8_device::is_long_opcode(UINT16 op)
285314{
286315   if((op & 0xf000) == 0x9000)
287316   {
r18789r18790
303332   return false;
304333}
305334
306INLINE UINT8 READ_PRG_8(avr8_state *cpustate, UINT32 address)
335//-------------------------------------------------
336//  device_start - start up the device
337//-------------------------------------------------
338
339void avr8_device::device_start()
307340{
308    return cpustate->program->read_byte(address);
341    m_pc = 0;
342
343    m_program = &space(AS_PROGRAM);
344    m_io = &space(AS_IO);
345
346   // register our state for the debugger
347   astring tempstr;
348   state_add(STATE_GENPC,     "GENPC",     m_pc).noshow();
349   state_add(STATE_GENFLAGS,  "GENFLAGS",  m_status).callimport().callexport().formatstr("%8s").noshow();
350   state_add(AVR8_SREG,       "STATUS",    m_r[AVR8_REGIDX_SREG]).mask(0xff);
351   state_add(AVR8_PC,         "PC",        m_debugger_pc).mask(0xffff);
352   state_add(AVR8_R0,         "R0",        m_r[ 0]).mask(0xff);
353   state_add(AVR8_R1,         "R1",        m_r[ 1]).mask(0xff);
354   state_add(AVR8_R2,         "R2",        m_r[ 2]).mask(0xff);
355   state_add(AVR8_R3,         "R3",        m_r[ 3]).mask(0xff);
356   state_add(AVR8_R4,         "R4",        m_r[ 4]).mask(0xff);
357   state_add(AVR8_R5,         "R5",        m_r[ 5]).mask(0xff);
358   state_add(AVR8_R6,         "R6",        m_r[ 6]).mask(0xff);
359   state_add(AVR8_R7,         "R7",        m_r[ 7]).mask(0xff);
360   state_add(AVR8_R8,         "R8",        m_r[ 8]).mask(0xff);
361   state_add(AVR8_R9,         "R9",        m_r[ 9]).mask(0xff);
362   state_add(AVR8_R10,        "R10",       m_r[10]).mask(0xff);
363   state_add(AVR8_R11,        "R11",       m_r[11]).mask(0xff);
364   state_add(AVR8_R12,        "R12",       m_r[12]).mask(0xff);
365   state_add(AVR8_R13,        "R13",       m_r[13]).mask(0xff);
366   state_add(AVR8_R14,        "R14",       m_r[14]).mask(0xff);
367   state_add(AVR8_R15,        "R15",       m_r[15]).mask(0xff);
368   state_add(AVR8_R16,        "R16",       m_r[16]).mask(0xff);
369   state_add(AVR8_R17,        "R17",       m_r[17]).mask(0xff);
370   state_add(AVR8_R18,        "R18",       m_r[18]).mask(0xff);
371   state_add(AVR8_R19,        "R19",       m_r[19]).mask(0xff);
372   state_add(AVR8_R20,        "R20",       m_r[20]).mask(0xff);
373   state_add(AVR8_R21,        "R21",       m_r[21]).mask(0xff);
374   state_add(AVR8_R22,        "R22",       m_r[22]).mask(0xff);
375   state_add(AVR8_R23,        "R23",       m_r[23]).mask(0xff);
376   state_add(AVR8_R24,        "R24",       m_r[24]).mask(0xff);
377   state_add(AVR8_R25,        "R25",       m_r[25]).mask(0xff);
378   state_add(AVR8_R26,        "R26",       m_r[26]).mask(0xff);
379   state_add(AVR8_R27,        "R27",       m_r[27]).mask(0xff);
380   state_add(AVR8_R28,        "R28",       m_r[28]).mask(0xff);
381   state_add(AVR8_R29,        "R29",       m_r[29]).mask(0xff);
382   state_add(AVR8_R30,        "R30",       m_r[30]).mask(0xff);
383   state_add(AVR8_R31,        "R31",       m_r[31]).mask(0xff);
384
385   // register our state for saving
386   save_item(NAME(m_pc));
387   save_item(NAME(m_status));
388   save_item(NAME(m_r));
389   save_item(NAME(m_timer0_top));
390   save_item(NAME(m_timer0_increment));
391   save_item(NAME(m_timer0_prescale));
392   save_item(NAME(m_timer0_prescale_count));
393   save_item(NAME(m_timer1_top));
394   save_item(NAME(m_timer1_increment));
395   save_item(NAME(m_timer1_prescale));
396   save_item(NAME(m_timer1_prescale_count));
397   save_item(NAME(m_timer2_top));
398   save_item(NAME(m_timer2_increment));
399   save_item(NAME(m_timer2_prescale));
400   save_item(NAME(m_timer2_prescale_count));
401   save_item(NAME(m_addr_mask));
402   save_item(NAME(m_interrupt_pending));
403   save_item(NAME(m_icount));
404   save_item(NAME(m_elapsed_cycles));
405
406   // set our instruction counter
407   m_icountptr = &m_icount;
309408}
310409
311INLINE UINT16 READ_PRG_16(avr8_state *cpustate, UINT32 address)
410//-------------------------------------------------
411//  device_reset - reset the device
412//-------------------------------------------------
413
414void avr8_device::device_reset()
312415{
313    return cpustate->program->read_word(address << 1);
416   m_status = 0;
417    io_write8(AVR8_REGIDX_SPL, 0);
418    io_write8(AVR8_REGIDX_SPH, 0);
419
420   for (int i = 0; i < 32; i++)
421   {
422      m_r[i] = 0;
423   }
424
425    m_timer0_top = 0;
426   m_timer0_increment = 1;
427   m_timer0_prescale = 0;
428   m_timer0_prescale_count = 0;
429
430    m_timer1_top = 0;
431   m_timer1_increment = 1;
432   m_timer1_prescale = 0;
433   m_timer1_prescale_count = 0;
434
435    m_timer2_top = 0;
436   m_timer2_increment = 1;
437   m_timer2_prescale = 0;
438   m_timer2_prescale_count = 0;
439
440    AVR8_TIMSK1 = 0;
441    AVR8_OCR1AH = 0;
442    AVR8_OCR1AL = 0;
443    AVR8_OCR1BH = 0;
444    AVR8_OCR1BL = 0;
445    AVR8_ICR1H = 0;
446    AVR8_ICR1L = 0;
447    AVR8_TCNT1H = 0;
448    AVR8_TCNT1L = 0;
449    AVR8_TCNT2 = 0;
450
451   m_interrupt_pending = false;
452
453   m_elapsed_cycles = 0;
314454}
315455
316INLINE void WRITE_PRG_8(avr8_state *cpustate, UINT32 address, UINT8 data)
456//-------------------------------------------------
457//  memory_space_config - return the configuration
458//  of the specified address space, or NULL if
459//  the space doesn't exist
460//-------------------------------------------------
461
462const address_space_config *avr8_device::memory_space_config(address_spacenum spacenum) const
317463{
318    cpustate->program->write_byte(address, data);
464   if (spacenum == AS_PROGRAM)
465   {
466      return &m_program_config;
467   }
468   else if (spacenum == AS_IO)
469   {
470      return &m_io_config;
471   }
472   return NULL;
319473}
320474
321INLINE void WRITE_PRG_16(avr8_state *cpustate, UINT32 address, UINT16 data)
475
476//-------------------------------------------------
477//  state_string_export - export state as a string
478//  for the debugger
479//-------------------------------------------------
480
481void avr8_device::state_string_export(const device_state_entry &entry, astring &string)
322482{
323    cpustate->program->write_word(address, data);
483   switch (entry.index())
484   {
485      case STATE_GENFLAGS:
486         string.printf("%c%c%c%c%c%c%c%c",
487             (m_status & 0x80) ? 'I' : '-',
488             (m_status & 0x40) ? 'T' : '-',
489             (m_status & 0x20) ? 'H' : '-',
490             (m_status & 0x10) ? 'S' : '-',
491             (m_status & 0x08) ? 'V' : '-',
492             (m_status & 0x04) ? 'N' : '-',
493             (m_status & 0x02) ? 'Z' : '-',
494             (m_status & 0x01) ? 'C' : '-');
495         break;
496   }
324497}
325498
326INLINE UINT8 READ_IO_8(avr8_state *cpustate, UINT16 address)
499
500//-------------------------------------------------
501//  disasm_min_opcode_bytes - return the length
502//  of the shortest instruction, in bytes
503//-------------------------------------------------
504
505UINT32 avr8_device::disasm_min_opcode_bytes() const
327506{
507   return 2;
508}
509
510
511//-------------------------------------------------
512//  disasm_max_opcode_bytes - return the length
513//  of the longest instruction, in bytes
514//-------------------------------------------------
515
516UINT32 avr8_device::disasm_max_opcode_bytes() const
517{
518   return 4;
519}
520
521
522//-------------------------------------------------
523//  disasm_disassemble - call the disassembly
524//  helper function
525//-------------------------------------------------
526
527offs_t avr8_device::disasm_disassemble(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram, UINT32 options)
528{
529   extern CPU_DISASSEMBLE( avr8 );
530   return CPU_DISASSEMBLE_NAME(avr8)(NULL, buffer, pc, oprom, opram, 0);
531}
532
533
534//**************************************************************************
535//  MEMORY ACCESSORS
536//**************************************************************************
537
538inline UINT8 avr8_device::program_read8(UINT32 address)
539{
540    return m_program->read_byte(address);
541}
542
543inline UINT16 avr8_device::program_read16(UINT32 address)
544{
545    return m_program->read_word(address << 1);
546}
547
548inline void avr8_device::program_write8(UINT32 address, UINT8 data)
549{
550    m_program->write_byte(address, data);
551}
552
553inline void avr8_device::program_write16(UINT32 address, UINT16 data)
554{
555    m_program->write_word(address, data);
556}
557
558inline UINT8 avr8_device::io_read8(UINT16 address)
559{
328560   if (address < 0x100)
329561   {
330562      // Allow unhandled internal registers to be handled by external driver
331563      UINT8 data;
332      if (avr8_io_reg_read(cpustate, address, &data))
564      if (io_reg_read(address, &data))
333565      {
334566         return data;
335567      }
336568   }
337    return cpustate->io->read_byte(address);
569    return m_io->read_byte(address);
338570}
339571
340INLINE void WRITE_IO_8(avr8_state *cpustate, UINT16 address, UINT8 data)
572inline void avr8_device::io_write8(UINT16 address, UINT8 data)
341573{
342574   if (address < 0x100)
343575   {
344576      // Allow unhandled internal registers to be handled by external driver
345      if (avr8_io_reg_write(cpustate, address, data))
577      if (io_reg_write(address, data))
346578      {
347579         return;
348580      }
349581   }
350    cpustate->io->write_byte(address, data);
582    m_io->write_byte(address, data);
351583}
352584
353INLINE void PUSH(avr8_state *cpustate, UINT8 val)
585inline void avr8_device::push(UINT8 val)
354586{
355587   UINT16 sp = SPREG;
356    WRITE_IO_8(cpustate, sp, val);
588    io_write8(sp, val);
357589    sp--;
358    WRITE_IO_8(cpustate, AVR8_REGIDX_SPL, sp & 0x00ff);
359    WRITE_IO_8(cpustate, AVR8_REGIDX_SPH, (sp >> 8) & 0x00ff);
590    io_write8(AVR8_REGIDX_SPL, sp & 0x00ff);
591    io_write8(AVR8_REGIDX_SPH, (sp >> 8) & 0x00ff);
360592}
361593
362INLINE UINT8 POP(avr8_state *cpustate)
594inline UINT8 avr8_device::pop()
363595{
364596   UINT16 sp = SPREG;
365597    sp++;
366   WRITE_IO_8(cpustate, AVR8_REGIDX_SPL, sp & 0x00ff);
367   WRITE_IO_8(cpustate, AVR8_REGIDX_SPH, (sp >> 8) & 0x00ff);
368    return READ_IO_8(cpustate, sp);
598   io_write8(AVR8_REGIDX_SPL, sp & 0x00ff);
599   io_write8(AVR8_REGIDX_SPH, (sp >> 8) & 0x00ff);
600    return io_read8(sp);
369601}
370602
371UINT64 avr8_get_elapsed_cycles(device_t *device)
372{
373   return get_safe_token(device)->elapsed_cycles;
374}
603//**************************************************************************
604//  IRQ HANDLING
605//**************************************************************************
375606
376/*****************************************************************************/
377// Interrupts
378
379static void avr8_set_irq_line(avr8_state *cpustate, UINT16 vector, int state)
607void avr8_device::set_irq_line(UINT16 vector, int state)
380608{
381609    // Horrible hack, not accurate
382610    if(state)
r18789r18790
384612      if(SREG_R(AVR8_SREG_I))
385613      {
386614         SREG_W(AVR8_SREG_I, 0);
387         //printf("Push: %04x\n", cpustate->pc);
388         PUSH(cpustate, (cpustate->pc >> 8) & 0x00ff);
389         PUSH(cpustate, cpustate->pc & 0x00ff);
390         cpustate->pc = vector;
391
392         //avr8_timer_tick(cpustate, 3);
615         push((m_pc >> 8) & 0x00ff);
616         push(m_pc & 0x00ff);
617         m_pc = vector;
393618      }
394619      else
395620      {
396         cpustate->interrupt_pending = true;
621         m_interrupt_pending = true;
397622      }
398623    }
399624}
r18789r18790
420645   { AVR8_INT_T2OVF,   AVR8_REGIDX_TIMSK2, AVR8_TIMSK2_TOIE2_MASK,  AVR8_REGIDX_TIFR2,   AVR8_TIFR2_TOV2_MASK }
421646};
422647
423static void avr8_update_interrupt_internal(avr8_state *cpustate, int source)
648void avr8_device::update_interrupt(int source)
424649{
425650    CInterruptCondition condition = s_int_conditions[source];
426651
427    int intstate = (cpustate->r[condition.m_regindex] & condition.m_regmask) ? 1 : 0;
428    intstate = (cpustate->r[condition.m_intreg] & condition.m_intmask) ? intstate : 0;
652    int intstate = (m_r[condition.m_regindex] & condition.m_regmask) ? 1 : 0;
653    intstate = (m_r[condition.m_intreg] & condition.m_intmask) ? intstate : 0;
429654
430   avr8_set_irq_line(cpustate, condition.m_intindex, intstate);
655   set_irq_line(condition.m_intindex, intstate);
431656
432657   if (intstate)
433658   {
434      cpustate->r[condition.m_regindex] &= ~condition.m_regmask;
659      m_r[condition.m_regindex] &= ~condition.m_regmask;
435660   }
436661}
437662
438void avr8_update_interrupt(device_t *device, int source)
439{
440   avr8_state *cpustate = get_safe_token(device);
441   avr8_update_interrupt_internal(cpustate, source);
442}
443663
444/*****************************************************************************/
445// Timers
446
447static void avr8_timer_tick(avr8_state *cpustate, int cycles)
664//**************************************************************************
665//  REGISTER HANDLING
666//**************************************************************************
667void avr8_device::timer_tick(int cycles)
448668{
449   if (cpustate->timer0_prescale != 0)
669   if (m_timer0_prescale != 0)
450670   {
451      cpustate->timer0_prescale_count += cycles;
452      while(cpustate->timer0_prescale_count >= cpustate->timer0_prescale)
671      m_timer0_prescale_count += cycles;
672      while(m_timer0_prescale_count >= m_timer0_prescale)
453673      {
454         avr8_timer0_tick(cpustate);
455         cpustate->timer0_prescale_count -= cpustate->timer0_prescale;
674         timer0_tick();
675         m_timer0_prescale_count -= m_timer0_prescale;
456676      }
457677   }
458678
459   if (cpustate->timer1_prescale != 0)
679   if (m_timer1_prescale != 0)
460680   {
461      cpustate->timer1_prescale_count += cycles;
462      while(cpustate->timer1_prescale_count >= cpustate->timer1_prescale)
681      m_timer1_prescale_count += cycles;
682      while(m_timer1_prescale_count >= m_timer1_prescale)
463683      {
464         avr8_timer1_tick(cpustate);
465         cpustate->timer1_prescale_count -= cpustate->timer1_prescale;
684         timer1_tick();
685         m_timer1_prescale_count -= m_timer1_prescale;
466686      }
467687   }
468688
469   if (cpustate->timer2_prescale != 0)
689   if (m_timer2_prescale != 0)
470690   {
471      cpustate->timer2_prescale_count += cycles;
472      while(cpustate->timer2_prescale_count >= (cpustate->timer2_prescale))
691      m_timer2_prescale_count += cycles;
692      while(m_timer2_prescale_count >= (m_timer2_prescale))
473693      {
474         avr8_timer2_tick(cpustate);
475         cpustate->timer2_prescale_count -= (cpustate->timer2_prescale);
694         timer2_tick();
695         m_timer2_prescale_count -= (m_timer2_prescale);
476696      }
477697   }
478698}
479699
480700// Timer 0 Handling
481static void avr8_timer0_tick(avr8_state *cpustate)
701void avr8_device::timer0_tick()
482702{
483703   // TODO
484704}
485705
486706// Timer 1 Handling
487707
488static void avr8_timer1_tick(avr8_state *cpustate)
708void avr8_device::timer1_tick()
489709{
490710    /* TODO: Handle comparison, setting OC1x pins, detection of BOTTOM and TOP */
491711
492    UINT16 count = (cpustate->r[AVR8_REGIDX_TCNT1H] << 8) | cpustate->r[AVR8_REGIDX_TCNT1L];
493    INT32 wgm1 = ((cpustate->r[AVR8_REGIDX_TCCR1B] & AVR8_TCCR1B_WGM1_32_MASK) >> 1) |
494                 (cpustate->r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_WGM1_10_MASK);
712    UINT16 count = (m_r[AVR8_REGIDX_TCNT1H] << 8) | m_r[AVR8_REGIDX_TCNT1L];
713    INT32 wgm1 = ((m_r[AVR8_REGIDX_TCCR1B] & AVR8_TCCR1B_WGM1_32_MASK) >> 1) |
714                 (m_r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_WGM1_10_MASK);
495715
496716    // Cache things in array form to avoid a compare+branch inside a potentially high-frequency timer
497    //UINT8 compare_mode[2] = { (cpustate->r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_COM1A_MASK) >> AVR8_TCCR1A_COM1A_SHIFT,
498                              //(cpustate->r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_COM1B_MASK) >> AVR8_TCCR1A_COM1B_SHIFT };
499    UINT16 ocr1[2] = { (cpustate->r[AVR8_REGIDX_OCR1AH] << 8) | cpustate->r[AVR8_REGIDX_OCR1AL],
500                       (cpustate->r[AVR8_REGIDX_OCR1BH] << 8) | cpustate->r[AVR8_REGIDX_OCR1BL] };
717    //UINT8 compare_mode[2] = { (m_r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_COM1A_MASK) >> AVR8_TCCR1A_COM1A_SHIFT,
718                              //(m_r[AVR8_REGIDX_TCCR1A] & AVR8_TCCR1A_COM1B_MASK) >> AVR8_TCCR1A_COM1B_SHIFT };
719    UINT16 ocr1[2] = { (m_r[AVR8_REGIDX_OCR1AH] << 8) | m_r[AVR8_REGIDX_OCR1AL],
720                       (m_r[AVR8_REGIDX_OCR1BH] << 8) | m_r[AVR8_REGIDX_OCR1BL] };
501721   UINT8 ocf1[2] = { (1 << AVR8_TIFR1_OCF1A_SHIFT), (1 << AVR8_TIFR1_OCF1B_SHIFT) };
502722   UINT8 int1[2] = { AVR8_INTIDX_OCF1A, AVR8_INTIDX_OCF1B };
503    INT32 increment = cpustate->timer1_increment;
723    INT32 increment = m_timer1_increment;
504724
505725    for(INT32 reg = AVR8_REG_A; reg <= AVR8_REG_B; reg++)
506726    {
r18789r18790
511731                {
512732                    if (reg == 0)
513733                    {
514                        cpustate->r[AVR8_REGIDX_TIFR1] |= AVR8_TIFR1_TOV1_MASK;
515                  avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_TOV1);
734                        m_r[AVR8_REGIDX_TIFR1] |= AVR8_TIFR1_TOV1_MASK;
735                  update_interrupt(AVR8_INTIDX_TOV1);
516736                        count = 0;
517737                        increment = 0;
518738                    }
519739
520                    cpustate->r[AVR8_REGIDX_TIFR1] |= ocf1[reg];
521               avr8_update_interrupt_internal(cpustate, int1[reg]);
740                    m_r[AVR8_REGIDX_TIFR1] |= ocf1[reg];
741               update_interrupt(int1[reg]);
522742                }
523743                else if(count == 0)
524744                {
525745                    if (reg == 0)
526746                    {
527                        cpustate->r[AVR8_REGIDX_TIFR1] &= ~AVR8_TIFR1_TOV1_MASK;
528                  avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_TOV1);
747                        m_r[AVR8_REGIDX_TIFR1] &= ~AVR8_TIFR1_TOV1_MASK;
748                  update_interrupt(AVR8_INTIDX_TOV1);
529749                    }
530750
531                    cpustate->r[AVR8_REGIDX_TIFR1] &= ~ocf1[reg];
532               avr8_update_interrupt_internal(cpustate, int1[reg]);
751                    m_r[AVR8_REGIDX_TIFR1] &= ~ocf1[reg];
752               update_interrupt(int1[reg]);
533753                }
534754                break;
535755
r18789r18790
541761        switch(compare_mode[reg])
542762        {
543763            case 0:
544                //verboselog(cpustate->pc, 0, "avr8_update_timer1_compare_mode: Normal port operation (OC1 disconnected)\n");
764                //verboselog(m_pc, 0, "update_timer1_compare_mode: Normal port operation (OC1 disconnected)\n");
545765                break;
546766
547767            case 1:
r18789r18790
556776    }
557777
558778    count += increment;
559    cpustate->r[AVR8_REGIDX_TCNT1H] = (count >> 8) & 0xff;
560    cpustate->r[AVR8_REGIDX_TCNT1L] = count & 0xff;
779    m_r[AVR8_REGIDX_TCNT1H] = (count >> 8) & 0xff;
780    m_r[AVR8_REGIDX_TCNT1L] = count & 0xff;
561781}
562782
563static void avr8_change_timsk1(avr8_state *cpustate, UINT8 data)
783void avr8_device::change_timsk1(UINT8 data)
564784{
565785   UINT8 oldtimsk = AVR8_TIMSK1;
566786   UINT8 newtimsk = data;
r18789r18790
571791   if(changed & AVR8_TIMSK1_ICIE1_MASK)
572792   {
573793      // Check for Input Capture Interrupt interrupt condition
574      avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_ICF1);
794      update_interrupt(AVR8_INTIDX_ICF1);
575795   }
576796
577797   if(changed & AVR8_TIMSK1_OCIE1B_MASK)
578798   {
579799      // Check for Output Compare B Interrupt interrupt condition
580      avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF1B);
800      update_interrupt(AVR8_INTIDX_OCF1B);
581801   }
582802
583803   if(changed & AVR8_TIMSK1_OCIE1A_MASK)
584804   {
585805      // Check for Output Compare A Interrupt interrupt condition
586      avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF1A);
806      update_interrupt(AVR8_INTIDX_OCF1A);
587807   }
588808
589809   if(changed & AVR8_TIMSK1_TOIE1_MASK)
590810   {
591811      // Check for Output Compare A Interrupt interrupt condition
592      avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_TOV1);
812      update_interrupt(AVR8_INTIDX_TOV1);
593813   }
594814}
595815
596static void avr8_update_timer1_waveform_gen_mode(avr8_state *cpustate)
816void avr8_device::update_timer1_waveform_gen_mode()
597817{
598818   // TODO
599   cpustate->timer1_top = 0;
600   verboselog(cpustate->pc, 0, "avr8_update_timer1_waveform_gen_mode: TODO; WGM1 is %d\n", AVR8_WGM1 );
819   m_timer1_top = 0;
820   verboselog(m_pc, 0, "update_timer1_waveform_gen_mode: TODO; WGM1 is %d\n", AVR8_WGM1 );
601821   switch(AVR8_WGM1)
602822   {
603823      case WGM1_NORMAL:
604         cpustate->timer1_top = 0xffff;
824         m_timer1_top = 0xffff;
605825         break;
606826
607827      case WGM1_PWM_8_PC:
608828      case WGM1_FAST_PWM_8:
609         cpustate->timer1_top = 0x00ff;
829         m_timer1_top = 0x00ff;
610830         break;
611831
612832      case WGM1_PWM_9_PC:
613833      case WGM1_FAST_PWM_9:
614         cpustate->timer1_top = 0x01ff;
834         m_timer1_top = 0x01ff;
615835         break;
616836
617837      case WGM1_PWM_10_PC:
618838      case WGM1_FAST_PWM_10:
619         cpustate->timer1_top = 0x03ff;
839         m_timer1_top = 0x03ff;
620840         break;
621841
622842      case WGM1_PWM_PFC_ICR:
623843      case WGM1_PWM_PC_ICR:
624844      case WGM1_CTC_ICR:
625845      case WGM1_FAST_PWM_ICR:
626         cpustate->timer1_top = AVR8_ICR1;
846         m_timer1_top = AVR8_ICR1;
627847         break;
628848
629849      case WGM1_PWM_PFC_OCR:
630850      case WGM1_PWM_PC_OCR:
631851      case WGM1_CTC_OCR:
632852      case WGM1_FAST_PWM_OCR:
633         cpustate->timer1_top = AVR8_OCR1A;
853         m_timer1_top = AVR8_OCR1A;
634854         break;
635855
636856      default:
637         verboselog(cpustate->pc, 0, "avr8_update_timer1_waveform_gen_mode: Unsupported waveform generation type: %d\n", AVR8_WGM1);
857         verboselog(m_pc, 0, "update_timer1_waveform_gen_mode: Unsupported waveform generation type: %d\n", AVR8_WGM1);
638858         break;
639859   }
640860}
641861
642static void avr8_changed_tccr1a(avr8_state *cpustate, UINT8 data)
862void avr8_device::changed_tccr1a(UINT8 data)
643863{
644864   UINT8 oldtccr = AVR8_TCCR1A;
645865   UINT8 newtccr = data;
646866   UINT8 changed = newtccr ^ oldtccr;
647867
648    cpustate->r[AVR8_REGIDX_TCCR1A] = newtccr;
868    m_r[AVR8_REGIDX_TCCR1A] = newtccr;
649869
650870   if(changed & AVR8_TCCR1A_WGM1_10_MASK)
651871   {
652872      // TODO
653      avr8_update_timer1_waveform_gen_mode(cpustate);
873      update_timer1_waveform_gen_mode();
654874   }
655875}
656876
657static void avr8_update_timer1_input_noise_canceler(avr8_state *cpustate)
877void avr8_device::update_timer1_input_noise_canceler()
658878{
659879   // TODO
660880}
661881
662static void avr8_update_timer1_input_edge_select(avr8_state *cpustate)
882void avr8_device::update_timer1_input_edge_select()
663883{
664884   // TODO
665   //verboselog(cpustate->pc, 0, "avr8_update_timer1_input_edge_select: TODO; Clocking edge is %s\n", "test");
885   //verboselog(m_pc, 0, "update_timer1_input_edge_select: TODO; Clocking edge is %s\n", "test");
666886}
667887
668static void avr8_update_timer1_clock_source(avr8_state *cpustate)
888void avr8_device::update_timer1_clock_source()
669889{
670890   switch(AVR8_TIMER1_CLOCK_SELECT)
671891   {
672892      case 0: // Counter stopped
673         cpustate->timer1_prescale = 0;
893         m_timer1_prescale = 0;
674894         break;
675895      case 1: // Clk/1; no prescaling
676         cpustate->timer1_prescale = 1;
896         m_timer1_prescale = 1;
677897         break;
678898      case 2: // Clk/8
679         cpustate->timer1_prescale = 8;
899         m_timer1_prescale = 8;
680900         break;
681901      case 3: // Clk/32
682         cpustate->timer1_prescale = 32;
902         m_timer1_prescale = 32;
683903         break;
684904      case 4: // Clk/64
685         cpustate->timer1_prescale = 64;
905         m_timer1_prescale = 64;
686906         break;
687907      case 5: // Clk/128
688         cpustate->timer1_prescale = 128;
908         m_timer1_prescale = 128;
689909         break;
690910      case 6: // T1 trigger, falling edge
691911      case 7: // T1 trigger, rising edge
692         cpustate->timer1_prescale = 0;
693         verboselog(cpustate->pc, 0, "avr8_update_timer1_clock_source: T1 Trigger mode not implemented yet\n");
912         m_timer1_prescale = 0;
913         verboselog(m_pc, 0, "update_timer1_clock_source: T1 Trigger mode not implemented yet\n");
694914         break;
695915   }
696916
697   if (cpustate->timer1_prescale_count > cpustate->timer1_prescale)
917   if (m_timer1_prescale_count > m_timer1_prescale)
698918   {
699      cpustate->timer1_prescale_count = cpustate->timer1_prescale - 1;
919      m_timer1_prescale_count = m_timer1_prescale - 1;
700920   }
701921}
702922
703static void avr8_changed_tccr1b(avr8_state *cpustate, UINT8 data)
923void avr8_device::changed_tccr1b(UINT8 data)
704924{
705925   UINT8 oldtccr = AVR8_TCCR1B;
706926   UINT8 newtccr = data;
707927   UINT8 changed = newtccr ^ oldtccr;
708928
709    cpustate->r[AVR8_REGIDX_TCCR1B] = newtccr;
929    m_r[AVR8_REGIDX_TCCR1B] = newtccr;
710930
711931   if(changed & AVR8_TCCR1B_ICNC1_MASK)
712932   {
713933      // TODO
714      avr8_update_timer1_input_noise_canceler(cpustate);
934      update_timer1_input_noise_canceler();
715935   }
716936
717937   if(changed & AVR8_TCCR1B_ICES1_MASK)
718938   {
719939      // TODO
720      avr8_update_timer1_input_edge_select(cpustate);
940      update_timer1_input_edge_select();
721941   }
722942
723943   if(changed & AVR8_TCCR1B_WGM1_32_MASK)
724944   {
725945      // TODO
726      avr8_update_timer1_waveform_gen_mode(cpustate);
946      update_timer1_waveform_gen_mode();
727947   }
728948
729949   if(changed & AVR8_TCCR1B_CS_MASK)
730950   {
731      avr8_update_timer1_clock_source(cpustate);
951      update_timer1_clock_source();
732952   }
733953}
734954
735static void avr8_update_ocr1(avr8_state *cpustate, UINT16 newval, UINT8 reg)
955void avr8_device::update_ocr1(UINT16 newval, UINT8 reg)
736956{
737   UINT8 *p_reg_h = (reg == AVR8_REG_A) ? &cpustate->r[AVR8_REGIDX_OCR1AH] : &cpustate->r[AVR8_REGIDX_OCR1BH];
738   UINT8 *p_reg_l = (reg == AVR8_REG_A) ? &cpustate->r[AVR8_REGIDX_OCR1AL] : &cpustate->r[AVR8_REGIDX_OCR1BL];
957   UINT8 *p_reg_h = (reg == AVR8_REG_A) ? &m_r[AVR8_REGIDX_OCR1AH] : &m_r[AVR8_REGIDX_OCR1BH];
958   UINT8 *p_reg_l = (reg == AVR8_REG_A) ? &m_r[AVR8_REGIDX_OCR1AL] : &m_r[AVR8_REGIDX_OCR1BL];
739959   *p_reg_h = (UINT8)(newval >> 8);
740960   *p_reg_l = (UINT8)newval;
741961
r18789r18790
744964
745965// Timer 2 Handling
746966
747static void avr8_timer2_tick(avr8_state *cpustate)
967void avr8_device::timer2_tick()
748968{
749    UINT16 count = cpustate->r[AVR8_REGIDX_TCNT2];
750    INT32 wgm2 = ((cpustate->r[AVR8_REGIDX_TCCR2B] & AVR8_TCCR2B_WGM2_2_MASK) >> 1) |
751                 (cpustate->r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_WGM2_10_MASK);
969    UINT16 count = m_r[AVR8_REGIDX_TCNT2];
970    INT32 wgm2 = ((m_r[AVR8_REGIDX_TCCR2B] & AVR8_TCCR2B_WGM2_2_MASK) >> 1) |
971                 (m_r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_WGM2_10_MASK);
752972
753973    // Cache things in array form to avoid a compare+branch inside a potentially high-frequency timer
754    //UINT8 compare_mode[2] = { (cpustate->r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_COM2A_MASK) >> AVR8_TCCR2A_COM2A_SHIFT,
755                              //(cpustate->r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_COM2B_MASK) >> AVR8_TCCR2A_COM2B_SHIFT };
756    UINT8 ocr2[2] = { cpustate->r[AVR8_REGIDX_OCR2A], cpustate->r[AVR8_REGIDX_OCR2B] };
974    //UINT8 compare_mode[2] = { (m_r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_COM2A_MASK) >> AVR8_TCCR2A_COM2A_SHIFT,
975                              //(m_r[AVR8_REGIDX_TCCR2A] & AVR8_TCCR2A_COM2B_MASK) >> AVR8_TCCR2A_COM2B_SHIFT };
976    UINT8 ocr2[2] = { m_r[AVR8_REGIDX_OCR2A], m_r[AVR8_REGIDX_OCR2B] };
757977   UINT8 ocf2[2] = { (1 << AVR8_TIFR2_OCF2A_SHIFT), (1 << AVR8_TIFR2_OCF2B_SHIFT) };
758    INT32 increment = cpustate->timer2_increment;
978    INT32 increment = m_timer2_increment;
759979
760980    for(INT32 reg = AVR8_REG_A; reg <= AVR8_REG_B; reg++)
761981    {
r18789r18790
766986                {
767987                    if (reg == 0)
768988                    {
769                        cpustate->r[AVR8_REGIDX_TIFR2] |= AVR8_TIFR2_TOV2_MASK;
989                        m_r[AVR8_REGIDX_TIFR2] |= AVR8_TIFR2_TOV2_MASK;
770990                        count = 0;
771991                        increment = 0;
772992                    }
773993
774                    cpustate->r[AVR8_REGIDX_TIFR2] |= ocf2[reg];
994                    m_r[AVR8_REGIDX_TIFR2] |= ocf2[reg];
775995                }
776996                else if(count == 0)
777997                {
778998                    if (reg == 0)
779999                    {
780                        cpustate->r[AVR8_REGIDX_TIFR2] &= ~AVR8_TIFR2_TOV2_MASK;
1000                        m_r[AVR8_REGIDX_TIFR2] &= ~AVR8_TIFR2_TOV2_MASK;
7811001                    }
7821002                }
7831003                break;
r18789r18790
7901010        switch(compare_mode[reg])
7911011        {
7921012            case 0:
793                //verboselog(cpustate->pc, 0, "avr8_update_timer2_compare_mode: Normal port operation (OC2 disconnected)\n");
1013                //verboselog(m_pc, 0, "update_timer2_compare_mode: Normal port operation (OC2 disconnected)\n");
7941014                break;
7951015
7961016            case 1:
r18789r18790
8041024        */
8051025    }
8061026
807    cpustate->r[AVR8_REGIDX_TCNT2] = count + increment;
1027    m_r[AVR8_REGIDX_TCNT2] = count + increment;
8081028
809   avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF2A);
810   avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF2B);
811   avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_TOV2);
1029   update_interrupt(AVR8_INTIDX_OCF2A);
1030   update_interrupt(AVR8_INTIDX_OCF2B);
1031   update_interrupt(AVR8_INTIDX_TOV2);
8121032}
8131033
814static void avr8_update_timer2_waveform_gen_mode(avr8_state *cpustate)
1034void avr8_device::update_timer2_waveform_gen_mode()
8151035{
816    cpustate->timer2_top = 0;
1036    m_timer2_top = 0;
8171037   switch(AVR8_WGM2)
8181038   {
8191039      case WGM2_NORMAL:
8201040      case WGM2_PWM_PC:
8211041      case WGM2_FAST_PWM:
822         cpustate->timer2_top = 0x00ff;
1042         m_timer2_top = 0x00ff;
8231043         break;
8241044
8251045      case WGM2_CTC_CMP:
8261046      case WGM2_PWM_PC_CMP:
8271047      case WGM2_FAST_PWM_CMP:
828         cpustate->timer2_top = AVR8_OCR2A;
1048         m_timer2_top = AVR8_OCR2A;
8291049         break;
8301050
8311051      default:
832         verboselog(cpustate->pc, 0, "avr8_update_timer2_waveform_gen_mode: Unsupported waveform generation type: %d\n", AVR8_WGM2);
1052         verboselog(m_pc, 0, "update_timer2_waveform_gen_mode: Unsupported waveform generation type: %d\n", AVR8_WGM2);
8331053         break;
8341054   }
8351055}
8361056
837static void avr8_changed_tccr2a(avr8_state *cpustate, UINT8 data)
1057void avr8_device::changed_tccr2a(UINT8 data)
8381058{
8391059   UINT8 oldtccr = AVR8_TCCR2A;
8401060   UINT8 newtccr = data;
r18789r18790
8431063   if(changed & AVR8_TCCR2A_WGM2_10_MASK)
8441064   {
8451065      // TODO
846      avr8_update_timer2_waveform_gen_mode(cpustate);
1066      update_timer2_waveform_gen_mode();
8471067   }
8481068}
8491069
850static void avr8_update_timer2_clock_source(avr8_state *cpustate)
1070void avr8_device::update_timer2_clock_source()
8511071{
8521072   switch(AVR8_TIMER2_CLOCK_SELECT)
8531073   {
8541074      case 0: // Counter stopped
855         cpustate->timer2_prescale = 0;
1075         m_timer2_prescale = 0;
8561076         break;
8571077      case 1: // Clk/1; no prescaling
858         cpustate->timer2_prescale = 1;
1078         m_timer2_prescale = 1;
8591079         break;
8601080      case 2: // Clk/8
861         cpustate->timer2_prescale = 8;
1081         m_timer2_prescale = 8;
8621082         break;
8631083      case 3: // Clk/32
864         cpustate->timer2_prescale = 32;
1084         m_timer2_prescale = 32;
8651085         break;
8661086      case 4: // Clk/64
867         cpustate->timer2_prescale = 64;
1087         m_timer2_prescale = 64;
8681088         break;
8691089      case 5: // Clk/128
870         cpustate->timer2_prescale = 128;
1090         m_timer2_prescale = 128;
8711091         break;
8721092      case 6: // Clk/256
873         cpustate->timer2_prescale = 256;
1093         m_timer2_prescale = 256;
8741094         break;
8751095      case 7: // Clk/1024
876         cpustate->timer2_prescale = 1024;
1096         m_timer2_prescale = 1024;
8771097         break;
8781098   }
8791099
880   if (cpustate->timer2_prescale_count > cpustate->timer2_prescale)
1100   if (m_timer2_prescale_count > m_timer2_prescale)
8811101   {
882      cpustate->timer2_prescale_count = cpustate->timer2_prescale - 1;
1102      m_timer2_prescale_count = m_timer2_prescale - 1;
8831103   }
8841104}
8851105
886static void avr8_timer2_force_output_compare(avr8_state *cpustate, int reg)
1106void avr8_device::timer2_force_output_compare(int reg)
8871107{
8881108   // TODO
889   verboselog(cpustate->pc, 0, "avr8_force_output_compare: TODO; should be forcing OC2%c\n", avr8_reg_name[reg]);
1109   verboselog(m_pc, 0, "force_output_compare: TODO; should be forcing OC2%c\n", avr8_reg_name[reg]);
8901110}
8911111
892static void avr8_changed_tccr2b(avr8_state *cpustate, UINT8 data)
1112void avr8_device::changed_tccr2b(UINT8 data)
8931113{
8941114   UINT8 oldtccr = AVR8_TCCR2B;
8951115   UINT8 newtccr = data;
r18789r18790
8981118   if(changed & AVR8_TCCR2B_FOC2A_MASK)
8991119   {
9001120      // TODO
901      avr8_timer2_force_output_compare(cpustate, AVR8_REG_A);
1121      timer2_force_output_compare(AVR8_REG_A);
9021122   }
9031123
9041124   if(changed & AVR8_TCCR2B_FOC2B_MASK)
9051125   {
9061126      // TODO
907      avr8_timer2_force_output_compare(cpustate, AVR8_REG_B);
1127      timer2_force_output_compare(AVR8_REG_B);
9081128   }
9091129
9101130   if(changed & AVR8_TCCR2B_WGM2_2_MASK)
9111131   {
9121132      // TODO
913      avr8_update_timer2_waveform_gen_mode(cpustate);
1133      update_timer2_waveform_gen_mode();
9141134   }
9151135
9161136   if(changed & AVR8_TCCR2B_CS_MASK)
9171137   {
918      avr8_update_timer2_clock_source(cpustate);
1138      update_timer2_clock_source();
9191139   }
9201140}
9211141
922static void avr8_update_ocr2(avr8_state *cpustate, UINT8 newval, UINT8 reg)
1142void avr8_device::update_ocr2(UINT8 newval, UINT8 reg)
9231143{
924    cpustate->r[(reg == AVR8_REG_A) ? AVR8_REGIDX_OCR2A : AVR8_REGIDX_OCR2B] = newval;
1144    m_r[(reg == AVR8_REG_A) ? AVR8_REGIDX_OCR2A : AVR8_REGIDX_OCR2B] = newval;
9251145
9261146    // Nothing needs to be done? All handled in timer callback
9271147}
9281148
9291149/*****************************************************************************/
9301150
931static bool avr8_io_reg_write(avr8_state *cpustate, UINT16 offset, UINT8 data)
1151bool avr8_device::io_reg_write(UINT16 offset, UINT8 data)
9321152{
9331153    switch( offset )
9341154    {
9351155      case AVR8_REGIDX_OCR1BH:
936         verboselog(cpustate->pc, 0, "AVR8: OCR1BH = %02x\n", data );
937         avr8_update_ocr1(cpustate, (AVR8_OCR1B & 0x00ff) | (data << 8), AVR8_REG_B);
1156         verboselog(m_pc, 0, "AVR8: OCR1BH = %02x\n", data );
1157         update_ocr1((AVR8_OCR1B & 0x00ff) | (data << 8), AVR8_REG_B);
9381158         return true;
9391159
9401160      case AVR8_REGIDX_OCR1BL:
941         verboselog(cpustate->pc, 0, "AVR8: OCR1BL = %02x\n", data );
942         avr8_update_ocr1(cpustate, (AVR8_OCR1B & 0xff00) | data, AVR8_REG_B);
1161         verboselog(m_pc, 0, "AVR8: OCR1BL = %02x\n", data );
1162         update_ocr1((AVR8_OCR1B & 0xff00) | data, AVR8_REG_B);
9431163         return true;
9441164
9451165      case AVR8_REGIDX_OCR1AH:
946         verboselog(cpustate->pc, 0, "AVR8: OCR1AH = %02x\n", data );
947         avr8_update_ocr1(cpustate, (AVR8_OCR1A & 0x00ff) | (data << 8), AVR8_REG_A);
1166         verboselog(m_pc, 0, "AVR8: OCR1AH = %02x\n", data );
1167         update_ocr1((AVR8_OCR1A & 0x00ff) | (data << 8), AVR8_REG_A);
9481168         return true;
9491169
9501170      case AVR8_REGIDX_OCR1AL:
951         verboselog(cpustate->pc, 0, "AVR8: OCR1AL = %02x\n", data );
952         avr8_update_ocr1(cpustate, (AVR8_OCR1A & 0xff00) | data, AVR8_REG_A);
1171         verboselog(m_pc, 0, "AVR8: OCR1AL = %02x\n", data );
1172         update_ocr1((AVR8_OCR1A & 0xff00) | data, AVR8_REG_A);
9531173         return true;
9541174
9551175      case AVR8_REGIDX_TCCR1B:
956         verboselog(cpustate->pc, 0, "AVR8: TCCR1B = %02x\n", data );
957         avr8_changed_tccr1b(cpustate, data);
1176         verboselog(m_pc, 0, "AVR8: TCCR1B = %02x\n", data );
1177         changed_tccr1b(data);
9581178         return true;
9591179
9601180      case AVR8_REGIDX_TCCR1A:
961         verboselog(cpustate->pc, 0, "AVR8: TCCR1A = %02x\n", data );
962         avr8_changed_tccr1a(cpustate, data);
1181         verboselog(m_pc, 0, "AVR8: TCCR1A = %02x\n", data );
1182         changed_tccr1a(data);
9631183         return true;
9641184
9651185      case AVR8_REGIDX_TIMSK1:
966         verboselog(cpustate->pc, 0, "AVR8: TIMSK1 = %02x\n", data );
967         avr8_change_timsk1(cpustate, data);
1186         verboselog(m_pc, 0, "AVR8: TIMSK1 = %02x\n", data );
1187         change_timsk1(data);
9681188         return true;
9691189
9701190      case AVR8_REGIDX_TIFR1:
971         verboselog(cpustate->pc, 0, "AVR8: TIFR1 = %02x\n", data );
972         cpustate->r[AVR8_REGIDX_TIFR1] &= ~(data & AVR8_TIFR1_MASK);
973         avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_ICF1);
974         avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF1A);
975         avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_OCF1B);
976         avr8_update_interrupt_internal(cpustate, AVR8_INTIDX_TOV1);
1191         verboselog(m_pc, 0, "AVR8: TIFR1 = %02x\n", data );
1192         m_r[AVR8_REGIDX_TIFR1] &= ~(data & AVR8_TIFR1_MASK);
1193         update_interrupt(AVR8_INTIDX_ICF1);
1194         update_interrupt(AVR8_INTIDX_OCF1A);
1195         update_interrupt(AVR8_INTIDX_OCF1B);
1196         update_interrupt(AVR8_INTIDX_TOV1);
9771197         return true;
9781198
9791199      case AVR8_REGIDX_TCCR2B:
980         verboselog(cpustate->pc, 0, "AVR8: TCCR2B = %02x\n", data );
981         avr8_changed_tccr2b(cpustate, data);
1200         verboselog(m_pc, 0, "AVR8: TCCR2B = %02x\n", data );
1201         changed_tccr2b(data);
9821202         return true;
9831203
9841204      case AVR8_REGIDX_TCCR2A:
985         verboselog(cpustate->pc, 0, "AVR8: TCCR2A = %02x\n", data );
986         avr8_changed_tccr2a(cpustate, data);
1205         verboselog(m_pc, 0, "AVR8: TCCR2A = %02x\n", data );
1206         changed_tccr2a(data);
9871207         return true;
9881208
9891209      case AVR8_REGIDX_OCR2A:
990         avr8_update_ocr2(cpustate, data, AVR8_REG_A);
1210         update_ocr2(data, AVR8_REG_A);
9911211         return true;
9921212
9931213      case AVR8_REGIDX_OCR2B:
994         avr8_update_ocr2(cpustate, data, AVR8_REG_B);
1214         update_ocr2(data, AVR8_REG_B);
9951215         return true;
9961216
9971217        case AVR8_REGIDX_TCNT2:
r18789r18790
10021222           if (data & AVR8_GTCCR_PSRASY_MASK)
10031223           {
10041224            data &= ~AVR8_GTCCR_PSRASY_MASK;
1005            cpustate->timer2_prescale_count = 0;
1225            m_timer2_prescale_count = 0;
10061226         }
1007            //verboselog(cpustate->pc, 0, "AVR8: GTCCR = %02x\n", data );
1227            //verboselog(m_pc, 0, "AVR8: GTCCR = %02x\n", data );
10081228            // TODO
10091229            return true;
10101230
10111231      case AVR8_REGIDX_SPL:
10121232      case AVR8_REGIDX_SPH:
1013         cpustate->r[offset] = data;
1233         m_r[offset] = data;
10141234         return true;
10151235
10161236      case AVR8_REGIDX_GPIOR0:
1017         verboselog(cpustate->pc, 0, "AVR8: GPIOR0 Write: %02x\n", data);
1018         cpustate->r[offset] = data;
1237         verboselog(m_pc, 0, "AVR8: GPIOR0 Write: %02x\n", data);
1238         m_r[offset] = data;
10191239         return true;
10201240
10211241      case AVR8_REGIDX_SREG:
1022         cpustate->status = data;
1242         m_status = data;
10231243         return true;
10241244
1245      case AVR8_REGIDX_PORTB:
1246         if (m_portb_changed)
1247         {
1248            UINT8 changed = m_r[AVR8_REGIDX_PORTB] ^ data;
1249            (*m_portb_changed)(*this, data, changed);
1250            m_r[AVR8_REGIDX_PORTB] = data;
1251         }
1252         break;
1253
1254      case AVR8_REGIDX_PORTC:
1255         if (m_portc_changed)
1256         {
1257            UINT8 changed = m_r[AVR8_REGIDX_PORTC] ^ data;
1258            (*m_portc_changed)(*this, data, changed);
1259            m_r[AVR8_REGIDX_PORTC] = data;
1260         }
1261         break;
1262
1263        case AVR8_REGIDX_PORTD:
1264         if (m_portd_changed)
1265         {
1266            UINT8 changed = m_r[AVR8_REGIDX_PORTD] ^ data;
1267            (*m_portd_changed)(*this, data, changed);
1268            m_r[AVR8_REGIDX_PORTD] = data;
1269         }
1270            break;
1271
10251272        default:
10261273            return false;
10271274    }
10281275    return false;
10291276}
10301277
1031static bool avr8_io_reg_read(avr8_state *cpustate, UINT16 offset, UINT8 *data)
1278bool avr8_device::io_reg_read(UINT16 offset, UINT8 *data)
10321279{
10331280    switch( offset )
10341281    {
r18789r18790
10371284      case AVR8_REGIDX_TCNT1L:
10381285      case AVR8_REGIDX_TCNT1H:
10391286      case AVR8_REGIDX_TCNT2:
1040         *data = cpustate->r[offset];
1287      case AVR8_REGIDX_PORTB:
1288      case AVR8_REGIDX_PORTC:
1289      case AVR8_REGIDX_PORTD:
1290         *data = m_r[offset];
10411291         return true;
10421292
10431293      case AVR8_REGIDX_GPIOR0:
1044         *data = cpustate->r[offset];
1045         verboselog(cpustate->pc, 0, "AVR8: GPIOR0 Read: %02x\n", *data);
1294         *data = m_r[offset];
1295         verboselog(m_pc, 0, "AVR8: GPIOR0 Read: %02x\n", *data);
10461296         return true;
10471297
10481298      case AVR8_REGIDX_SREG:
1049         *data = cpustate->status;
1299         *data = m_status;
10501300         return true;
10511301
10521302        default:
r18789r18790
10561306    return false;
10571307}
10581308
1059/*****************************************************************************/
10601309
1061static CPU_INIT( avr8 )
1062{
1063    avr8_state *cpustate = get_safe_token(device);
1310//**************************************************************************
1311//  CORE EXECUTION LOOP
1312//**************************************************************************
10641313
1065    cpustate->pc = 0;
1314//-------------------------------------------------
1315//  execute_min_cycles - return minimum number of
1316//  cycles it takes for one instruction to execute
1317//-------------------------------------------------
10661318
1067    cpustate->device = device;
1068    cpustate->program = &device->space(AS_PROGRAM);
1069    cpustate->io = &device->space(AS_IO);
1319UINT32 avr8_device::execute_min_cycles() const
1320{
1321   return 1;
1322}
10701323
1071   cpustate->status = 0;
1072    WRITE_IO_8(cpustate, AVR8_REGIDX_SPL, 0);
1073    WRITE_IO_8(cpustate, AVR8_REGIDX_SPH, 0);
10741324
1075   for (int i = 0; i < 32; i++)
1076   {
1077      cpustate->r[i] = 0;
1078   }
1325//-------------------------------------------------
1326//  execute_max_cycles - return maximum number of
1327//  cycles it takes for one instruction to execute
1328//-------------------------------------------------
10791329
1080    cpustate->timer0_top = 0;
1081   cpustate->timer0_increment = 1;
1082   cpustate->timer0_prescale = 0;
1083   cpustate->timer0_prescale_count = 0;
1330UINT32 avr8_device::execute_max_cycles() const
1331{
1332   return 4;
1333}
10841334
1085    cpustate->timer1_top = 0;
1086   cpustate->timer1_increment = 1;
1087   cpustate->timer1_prescale = 0;
1088   cpustate->timer1_prescale_count = 0;
10891335
1090    cpustate->timer2_top = 0;
1091   cpustate->timer2_increment = 1;
1092   cpustate->timer2_prescale = 0;
1093   cpustate->timer2_prescale_count = 0;
1336//-------------------------------------------------
1337//  execute_input_lines - return the number of
1338//  input/interrupt lines
1339//-------------------------------------------------
10941340
1095    AVR8_TIMSK1 = 0;
1096    AVR8_OCR1AH = 0;
1097    AVR8_OCR1AL = 0;
1098    AVR8_OCR1BH = 0;
1099    AVR8_OCR1BL = 0;
1100    AVR8_ICR1H = 0;
1101    AVR8_ICR1L = 0;
1102    AVR8_TCNT1H = 0;
1103    AVR8_TCNT1L = 0;
1104    AVR8_TCNT2 = 0;
1341UINT32 avr8_device::execute_input_lines() const
1342{
1343   return 0;
1344}
11051345
1106   cpustate->interrupt_pending = false;
11071346
1108   cpustate->elapsed_cycles = 0;
1109
1110   device->save_item(NAME(cpustate->pc));
1111}
1112
1113static CPU_EXIT( avr8 )
1347void avr8_device::execute_set_input(int inputnum, int state)
11141348{
11151349}
11161350
1117static CPU_RESET( avr8 )
1118{
1119    avr8_state *cpustate = get_safe_token(device);
11201351
1121   cpustate->status = 0;
1122    cpustate->pc = 0;
1123    cpustate->elapsed_cycles = 0;
1352//-------------------------------------------------
1353//  execute_run - execute a timeslice's worth of
1354//  opcodes
1355//-------------------------------------------------
11241356
1125    cpustate->interrupt_pending = false;
1126}
1127
1128static CPU_EXECUTE( avr8 )
1357void avr8_device::execute_run()
11291358{
11301359    UINT32 op = 0;
11311360    INT32 offs = 0;
r18789r18790
11351364    UINT16 pd = 0;
11361365    INT16 sd = 0;
11371366    INT32 opcycles = 1;
1138    //UINT16 pr = 0;
1139    avr8_state *cpustate = get_safe_token(device);
11401367
1141    while (cpustate->icount > 0)
1368    while (m_icount > 0)
11421369    {
11431370      opcycles = 1;
11441371
1145        cpustate->pc &= cpustate->addr_mask;
1372        m_pc &= m_addr_mask;
11461373
1147        debugger_instruction_hook(device, cpustate->pc << 1);
1374        debugger_instruction_hook(this, m_debugger_pc);
11481375
1149        op = (UINT32)READ_PRG_16(cpustate, cpustate->pc);
1376        op = (UINT32)program_read16(m_pc);
11501377
11511378        switch(op & 0xf000)
11521379        {
r18789r18790
11561383                    case 0x0000:    // NOP
11571384                        break;
11581385                    case 0x0100:    // MOVW Rd+1:Rd,Rr+1:Rd
1159                       cpustate->r[(RD4(op) << 1) + 1] = cpustate->r[(RR4(op) << 1) + 1];
1160                       cpustate->r[RD4(op) << 1] = cpustate->r[RR4(op) << 1];
1386                       m_r[(RD4(op) << 1) + 1] = m_r[(RR4(op) << 1) + 1];
1387                       m_r[RD4(op) << 1] = m_r[RR4(op) << 1];
11611388                        break;
11621389                    case 0x0200:    // MULS Rd,Rr
1163                        sd = (INT8)cpustate->r[16 + RD4(op)] * (INT8)cpustate->r[16 + RR4(op)];
1164                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1165                        cpustate->r[0] = sd & 0x00ff;
1390                        sd = (INT8)m_r[16 + RD4(op)] * (INT8)m_r[16 + RR4(op)];
1391                        m_r[1] = (sd >> 8) & 0x00ff;
1392                        m_r[0] = sd & 0x00ff;
11661393                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
11671394                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
11681395                        opcycles = 2;
r18789r18790
11711398                       switch(MULCONST2(op))
11721399                       {
11731400                     case 0x0000: // MULSU Rd,Rr
1174                        sd = (INT8)cpustate->r[16 + RD3(op)] * (UINT8)cpustate->r[16 + RR3(op)];
1175                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1176                        cpustate->r[0] = sd & 0x00ff;
1401                        sd = (INT8)m_r[16 + RD3(op)] * (UINT8)m_r[16 + RR3(op)];
1402                        m_r[1] = (sd >> 8) & 0x00ff;
1403                        m_r[0] = sd & 0x00ff;
11771404                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
11781405                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
11791406                        opcycles = 2;
11801407                        break;
11811408                     case 0x0001: // FMUL Rd,Rr
1182                        sd = (UINT8)cpustate->r[16 + RD3(op)] * (UINT8)cpustate->r[16 + RR3(op)];
1409                        sd = (UINT8)m_r[16 + RD3(op)] * (UINT8)m_r[16 + RR3(op)];
11831410                        sd <<= 1;
1184                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1185                        cpustate->r[0] = sd & 0x00ff;
1411                        m_r[1] = (sd >> 8) & 0x00ff;
1412                        m_r[0] = sd & 0x00ff;
11861413                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
11871414                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
11881415                        opcycles = 2;
11891416                        break;
11901417                     case 0x0002: // FMULS Rd,Rr
1191                        sd = (INT8)cpustate->r[16 + RD3(op)] * (INT8)cpustate->r[16 + RR3(op)];
1418                        sd = (INT8)m_r[16 + RD3(op)] * (INT8)m_r[16 + RR3(op)];
11921419                        sd <<= 1;
1193                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1194                        cpustate->r[0] = sd & 0x00ff;
1420                        m_r[1] = (sd >> 8) & 0x00ff;
1421                        m_r[0] = sd & 0x00ff;
11951422                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
11961423                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
11971424                        opcycles = 2;
11981425                        break;
11991426                     case 0x0003: // FMULSU Rd,Rr
1200                        sd = (INT8)cpustate->r[16 + RD3(op)] * (UINT8)cpustate->r[16 + RR3(op)];
1427                        sd = (INT8)m_r[16 + RD3(op)] * (UINT8)m_r[16 + RR3(op)];
12011428                        sd <<= 1;
1202                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1203                        cpustate->r[0] = sd & 0x00ff;
1429                        m_r[1] = (sd >> 8) & 0x00ff;
1430                        m_r[0] = sd & 0x00ff;
12041431                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
12051432                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
12061433                        opcycles = 2;
r18789r18790
12111438                    case 0x0500:
12121439                    case 0x0600:
12131440                    case 0x0700:    // CPC Rd,Rr
1214                        rd = cpustate->r[RD5(op)];
1215                        rr = cpustate->r[RR5(op)];
1441                        rd = m_r[RD5(op)];
1442                        rr = m_r[RR5(op)];
12161443                        res = rd - (rr + SREG_R(AVR8_SREG_C));
12171444                        SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
12181445                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
r18789r18790
12251452                    case 0x0900:
12261453                    case 0x0a00:
12271454                    case 0x0b00:    // SBC Rd,Rr
1228                        rd = cpustate->r[RD5(op)];
1229                        rr = cpustate->r[RR5(op)];
1455                        rd = m_r[RD5(op)];
1456                        rr = m_r[RR5(op)];
12301457                        res = rd - (rr + SREG_R(AVR8_SREG_C));
1231                        cpustate->r[RD5(op)] = res;
1458                        m_r[RD5(op)] = res;
12321459                        SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
12331460                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
12341461                        SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
12401467                    case 0x0d00:
12411468                    case 0x0e00:
12421469                    case 0x0f00:    // ADD Rd,Rr
1243                        rd = cpustate->r[RD5(op)];
1244                        rr = cpustate->r[RR5(op)];
1470                        rd = m_r[RD5(op)];
1471                        rr = m_r[RR5(op)];
12451472                        res = rd + rr;
1246                        cpustate->r[RD5(op)] = res;
1473                        m_r[RD5(op)] = res;
12471474                        SREG_W(AVR8_SREG_H, (BIT(rd,3) & BIT(rr,3)) | (BIT(rr,3) & NOT(BIT(res,3))) | (NOT(BIT(res,3)) & BIT(rd,3)));
12481475                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & BIT(rr,7) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & NOT(BIT(rr,7)) & BIT(res,7)));
12491476                        SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
12581485                {
12591486                    case 0x0000:    // CPSR Rd,Rr
12601487                        //output += sprintf( output, "CPSE    R%d, R%d", RD5(op), RR5(op) );
1261                        unimplemented_opcode(cpustate, op);
1488                        unimplemented_opcode(op);
12621489                        break;
12631490                    case 0x0400:    // CP Rd,Rr
1264                        rd = cpustate->r[RD5(op)];
1265                        rr = cpustate->r[RR5(op)];
1491                        rd = m_r[RD5(op)];
1492                        rr = m_r[RR5(op)];
12661493                        res = rd - rr;
12671494                        SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
12681495                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
r18789r18790
12721499                        SREG_W(AVR8_SREG_C, (NOT(BIT(rd,7)) & BIT(rr,7)) | (BIT(rr,7) & BIT(res,7)) | (BIT(res,7) & NOT(BIT(rd,7))));
12731500                        break;
12741501                    case 0x0800:    // SUB Rd,Rr
1275                        rd = cpustate->r[RD5(op)];
1276                        rr = cpustate->r[RR5(op)];
1502                        rd = m_r[RD5(op)];
1503                        rr = m_r[RR5(op)];
12771504                        res = rd - rr;
1278                        cpustate->r[RD5(op)] = res;
1505                        m_r[RD5(op)] = res;
12791506                        SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
12801507                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
12811508                        SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
12841511                        SREG_W(AVR8_SREG_C, (NOT(BIT(rd,7)) & BIT(rr,7)) | (BIT(rr,7) & BIT(res,7)) | (BIT(res,7) & NOT(BIT(rd,7))));
12851512                        break;
12861513                    case 0x0c00:    // ADC Rd,Rr
1287                        rd = cpustate->r[RD5(op)];
1288                        rr = cpustate->r[RR5(op)];
1514                        rd = m_r[RD5(op)];
1515                        rr = m_r[RR5(op)];
12891516                        res = rd + rr + SREG_R(AVR8_SREG_C);
1290                        cpustate->r[RD5(op)] = res;
1517                        m_r[RD5(op)] = res;
12911518                        SREG_W(AVR8_SREG_H, (BIT(rd,3) & BIT(rr,3)) | (BIT(rr,3) & NOT(BIT(res,3))) | (NOT(BIT(res,3)) & BIT(rd,3)));
12921519                        SREG_W(AVR8_SREG_V, (BIT(rd,7) & BIT(rr,7) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & NOT(BIT(rr,7)) & BIT(res,7)));
12931520                        SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
13011528                switch(op & 0x0c00)
13021529                {
13031530                    case 0x0000:    // AND Rd,Rr
1304                        rd = cpustate->r[RD5(op)];
1305                        rr = cpustate->r[RR5(op)];
1531                        rd = m_r[RD5(op)];
1532                        rr = m_r[RR5(op)];
13061533                        rd &= rr;
13071534                        SREG_W(AVR8_SREG_V, 0);
13081535                        SREG_W(AVR8_SREG_N, BIT(rd,7));
13091536                        SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
13101537                        SREG_W(AVR8_SREG_Z, (rd == 0) ? 1 : 0);
1311                        cpustate->r[RD5(op)] = rd;
1538                        m_r[RD5(op)] = rd;
13121539                        break;
13131540                    case 0x0400:    // EOR Rd,Rr
1314                        rd = cpustate->r[RD5(op)];
1315                        rr = cpustate->r[RR5(op)];
1541                        rd = m_r[RD5(op)];
1542                        rr = m_r[RR5(op)];
13161543                        rd ^= rr;
13171544                        SREG_W(AVR8_SREG_V, 0);
13181545                        SREG_W(AVR8_SREG_N, BIT(rd,7));
13191546                        SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
13201547                        SREG_W(AVR8_SREG_Z, (rd == 0) ? 1 : 0);
1321                        cpustate->r[RD5(op)] = rd;
1548                        m_r[RD5(op)] = rd;
13221549                        break;
13231550                    case 0x0800:    // OR Rd,Rr
1324                        rd = cpustate->r[RD5(op)];
1325                        rr = cpustate->r[RR5(op)];
1551                        rd = m_r[RD5(op)];
1552                        rr = m_r[RR5(op)];
13261553                        rd |= rr;
13271554                        SREG_W(AVR8_SREG_V, 0);
13281555                        SREG_W(AVR8_SREG_N, BIT(rd,7));
13291556                        SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
13301557                        SREG_W(AVR8_SREG_Z, (rd == 0) ? 1 : 0);
1331                        cpustate->r[RD5(op)] = rd;
1558                        m_r[RD5(op)] = rd;
13321559                        break;
13331560                    case 0x0c00:    // MOV Rd,Rr
1334                       cpustate->r[RD5(op)] = cpustate->r[RR5(op)];
1561                       m_r[RD5(op)] = m_r[RR5(op)];
13351562                        break;
13361563                }
13371564                break;
13381565            case 0x3000:    // CPI Rd,K
1339                rd = cpustate->r[16 + RD4(op)];
1566                rd = m_r[16 + RD4(op)];
13401567                rr = KCONST8(op);
13411568                res = rd - rr;
13421569                SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
r18789r18790
13471574                SREG_W(AVR8_SREG_C, (NOT(BIT(rd,7)) & BIT(rr,7)) | (BIT(rr,7) & BIT(res,7)) | (BIT(res,7) & NOT(BIT(rd,7))));
13481575                break;
13491576            case 0x4000:    // SBCI Rd,K
1350                rd = cpustate->r[16 + RD4(op)];
1577                rd = m_r[16 + RD4(op)];
13511578                rr = KCONST8(op);
13521579                res = rd - (rr + SREG_R(AVR8_SREG_C));
1353                cpustate->r[16 + RD4(op)] = res;
1580                m_r[16 + RD4(op)] = res;
13541581                SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
13551582                SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
13561583                SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
13591586                SREG_W(AVR8_SREG_C, (NOT(BIT(rd,7)) & BIT(rr,7)) | (BIT(rr,7) & BIT(res,7)) | (BIT(res,7) & NOT(BIT(rd,7))));
13601587                break;
13611588            case 0x5000:    // SUBI Rd,K
1362                rd = cpustate->r[16 + RD4(op)];
1589                rd = m_r[16 + RD4(op)];
13631590                rr = KCONST8(op);
13641591                res = rd - rr;
1365                cpustate->r[16 + RD4(op)] = res;
1592                m_r[16 + RD4(op)] = res;
13661593                SREG_W(AVR8_SREG_H, (NOT(BIT(rd,3)) & BIT(rr,3)) | (BIT(rr,3) & BIT(res,3)) | (BIT(res,3) & NOT(BIT(rd,3))));
13671594                SREG_W(AVR8_SREG_V, (BIT(rd,7) & NOT(BIT(rr,7)) & NOT(BIT(res,7))) | (NOT(BIT(rd,7)) & BIT(rr,7) & BIT(res,7)));
13681595                SREG_W(AVR8_SREG_N, BIT(res,7));
r18789r18790
13711598                SREG_W(AVR8_SREG_C, (NOT(BIT(rd,7)) & BIT(rr,7)) | (BIT(rr,7) & BIT(res,7)) | (BIT(res,7) & NOT(BIT(rd,7))));
13721599                break;
13731600            case 0x6000:    // ORI Rd,K
1374                rd = cpustate->r[16 + RD4(op)];
1601                rd = m_r[16 + RD4(op)];
13751602                rr = KCONST8(op);
13761603                rd |= rr;
13771604                SREG_W(AVR8_SREG_V, 0);
13781605                SREG_W(AVR8_SREG_N, BIT(rd,7));
13791606                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
13801607                SREG_W(AVR8_SREG_Z, (rd == 0) ? 1 : 0);
1381                cpustate->r[16 + RD4(op)] = rd;
1608                m_r[16 + RD4(op)] = rd;
13821609                break;
13831610            case 0x7000:    // ANDI Rd,K
1384                rd = cpustate->r[16 + RD4(op)];
1611                rd = m_r[16 + RD4(op)];
13851612                rr = KCONST8(op);
13861613                rd &= rr;
13871614                SREG_W(AVR8_SREG_V, 0);
13881615                SREG_W(AVR8_SREG_N, BIT(rd,7));
13891616                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
13901617                SREG_W(AVR8_SREG_Z, (rd == 0) ? 1 : 0);
1391                cpustate->r[16 + RD4(op)] = rd;
1618                m_r[16 + RD4(op)] = rd;
13921619                break;
13931620            case 0x8000:
13941621            case 0xa000:
13951622                switch(op & 0x0208)
13961623                {
13971624                    case 0x0000:    // LDD Rd,Z+q
1398                        cpustate->r[RD5(op)] = READ_IO_8(cpustate, ZREG + QCONST6(op));
1625                        m_r[RD5(op)] = io_read8(ZREG + QCONST6(op));
13991626                        opcycles = 2;
14001627                        break;
14011628                    case 0x0008:    // LDD Rd,Y+q
1402                        cpustate->r[RD5(op)] = READ_IO_8(cpustate, YREG + QCONST6(op));
1629                        m_r[RD5(op)] = io_read8(YREG + QCONST6(op));
14031630                        opcycles = 2;
14041631                        break;
14051632                    case 0x0200:    // STD Z+q,Rr
1406                        WRITE_IO_8(cpustate, ZREG + QCONST6(op), cpustate->r[RD5(op)]);
1633                        io_write8(ZREG + QCONST6(op), m_r[RD5(op)]);
14071634                        opcycles = 2;
14081635                        break;
14091636                    case 0x0208:    // STD Y+q,Rr
1410                        WRITE_IO_8(cpustate, YREG + QCONST6(op), cpustate->r[RD5(op)]);
1637                        io_write8(YREG + QCONST6(op), m_r[RD5(op)]);
14111638                        opcycles = 2;
14121639                        break;
14131640                }
r18789r18790
14211648                        {
14221649                            case 0x0000:    // LDS Rd,k
14231650                                op <<= 16;
1424                                cpustate->pc++;
1425                                op |= READ_PRG_16(cpustate, cpustate->pc);
1426                                cpustate->r[RD5(op >> 16)] = READ_IO_8(cpustate, op & 0x0000ffff);
1651                                m_pc++;
1652                                op |= program_read16(m_pc);
1653                                m_r[RD5(op >> 16)] = io_read8(op & 0x0000ffff);
14271654                                opcycles = 2;
14281655                                break;
14291656                            case 0x0001:    // LD Rd,Z+
1430                                unimplemented_opcode(cpustate, op);
1657                                unimplemented_opcode(op);
14311658                                break;
14321659                            case 0x0002:    // LD Rd,-Z
14331660                                pd = ZREG;
14341661                                pd--;
1435                                cpustate->r[RD5(op)] = READ_IO_8(cpustate, pd);
1436                                cpustate->r[31] = (pd >> 8) & 0x00ff;
1437                                cpustate->r[30] = pd & 0x00ff;
1662                                m_r[RD5(op)] = io_read8(pd);
1663                                m_r[31] = (pd >> 8) & 0x00ff;
1664                                m_r[30] = pd & 0x00ff;
14381665                                opcycles = 2;
14391666                                break;
14401667                            case 0x0004:    // LPM Rd,Z
1441                                cpustate->r[RD5(op)] = READ_PRG_8(cpustate, ZREG);
1668                                m_r[RD5(op)] = program_read8(ZREG);
14421669                                opcycles = 3;
14431670                                break;
14441671                            case 0x0005:    // LPM Rd,Z+
14451672                                pd = ZREG;
1446                                cpustate->r[RD5(op)] = READ_PRG_8(cpustate, pd);
1673                                m_r[RD5(op)] = program_read8(pd);
14471674                                pd++;
1448                                cpustate->r[31] = (pd >> 8) & 0x00ff;
1449                                cpustate->r[30] = pd & 0x00ff;
1675                                m_r[31] = (pd >> 8) & 0x00ff;
1676                                m_r[30] = pd & 0x00ff;
14501677                                opcycles = 3;
14511678                                break;
14521679                            case 0x0006:    // ELPM Rd,Z
14531680                                //output += sprintf( output, "ELPM    R%d, Z", RD5(op) );
1454                                unimplemented_opcode(cpustate, op);
1681                                unimplemented_opcode(op);
14551682                                break;
14561683                            case 0x0007:    // ELPM Rd,Z+
14571684                                //output += sprintf( output, "ELPM    R%d, Z+", RD5(op) );
1458                                unimplemented_opcode(cpustate, op);
1685                                unimplemented_opcode(op);
14591686                                break;
14601687                            case 0x0009:    // LD Rd,Y+
14611688                                pd = YREG;
1462                                cpustate->r[RD5(op)] = READ_IO_8(cpustate, pd);
1689                                m_r[RD5(op)] = io_read8(pd);
14631690                                pd++;
1464                                cpustate->r[29] = (pd >> 8) & 0x00ff;
1465                                cpustate->r[28] = pd & 0x00ff;
1691                                m_r[29] = (pd >> 8) & 0x00ff;
1692                                m_r[28] = pd & 0x00ff;
14661693                                opcycles = 2;
14671694                                break;
14681695                            case 0x000a:    // LD Rd,-Y
14691696                                pd = YREG;
14701697                                pd--;
1471                                cpustate->r[RD5(op)] = READ_IO_8(cpustate, pd);
1472                                cpustate->r[29] = (pd >> 8) & 0x00ff;
1473                                cpustate->r[28] = pd & 0x00ff;
1698                                m_r[RD5(op)] = io_read8(pd);
1699                                m_r[29] = (pd >> 8) & 0x00ff;
1700                                m_r[28] = pd & 0x00ff;
14741701                                opcycles = 2;
14751702                                break;
14761703                            case 0x000c:    // LD Rd,X
1477                               cpustate->r[RD5(op)] = READ_IO_8(cpustate, XREG);
1704                               m_r[RD5(op)] = io_read8(XREG);
14781705                               opcycles = 2;
14791706                                break;
14801707                            case 0x000d:    // LD Rd,X+
14811708                                pd = XREG;
1482                                cpustate->r[RD5(op)] = READ_IO_8(cpustate, pd);
1709                                m_r[RD5(op)] = io_read8(pd);
14831710                                pd++;
1484                                cpustate->r[27] = (pd >> 8) & 0x00ff;
1485                                cpustate->r[26] = pd & 0x00ff;
1711                                m_r[27] = (pd >> 8) & 0x00ff;
1712                                m_r[26] = pd & 0x00ff;
14861713                                opcycles = 2;
14871714                                break;
14881715                            case 0x000e:    // LD Rd,-X
14891716                                pd = XREG;
14901717                                pd--;
1491                                cpustate->r[RD5(op)] = READ_IO_8(cpustate, pd);
1492                                cpustate->r[27] = (pd >> 8) & 0x00ff;
1493                                cpustate->r[26] = pd & 0x00ff;
1718                                m_r[RD5(op)] = io_read8(pd);
1719                                m_r[27] = (pd >> 8) & 0x00ff;
1720                                m_r[26] = pd & 0x00ff;
14941721                                opcycles = 2;
14951722                                break;
14961723                            case 0x000f:    // POP Rd
1497                                cpustate->r[RD5(op)] = POP(cpustate);
1724                                m_r[RD5(op)] = pop();
14981725                                opcycles = 2;
14991726                                break;
15001727                            default:
1501                                unimplemented_opcode(cpustate, op);
1728                                unimplemented_opcode(op);
15021729                                //output += sprintf( output, "Undefined (%04x)", op );
15031730                                break;
15041731                        }
r18789r18790
15091736                        {
15101737                            case 0x0000:    // STS k,Rr
15111738                                op <<= 16;
1512                                cpustate->pc++;
1513                                op |= READ_PRG_16(cpustate, cpustate->pc);
1514                                WRITE_IO_8(cpustate, op & 0x0000ffff, cpustate->r[RD5(op >> 16)]);
1739                                m_pc++;
1740                                op |= program_read16(m_pc);
1741                                io_write8(op & 0x0000ffff, m_r[RD5(op >> 16)]);
15151742                                opcycles = 2;
15161743                                break;
15171744                            case 0x0001:    // ST Z+,Rd
15181745                                //output += sprintf( output, "ST       Z+, R%d", RD5(op) );
1519                                unimplemented_opcode(cpustate, op);
1746                                unimplemented_opcode(op);
15201747                                break;
15211748                            case 0x0002:    // ST -Z,Rd
15221749                                //output += sprintf( output, "ST      -Z , R%d", RD5(op) );
1523                                unimplemented_opcode(cpustate, op);
1750                                unimplemented_opcode(op);
15241751                                break;
15251752                            case 0x0009:    // ST Y+,Rd
15261753                                pd = YREG;
1527                                WRITE_IO_8(cpustate, pd, cpustate->r[RD5(op)]);
1754                                io_write8(pd, m_r[RD5(op)]);
15281755                                pd++;
1529                                cpustate->r[29] = (pd >> 8) & 0x00ff;
1530                                cpustate->r[28] = pd & 0x00ff;
1756                                m_r[29] = (pd >> 8) & 0x00ff;
1757                                m_r[28] = pd & 0x00ff;
15311758                                opcycles = 2;
15321759                                break;
1533                            case 0x000a:    // ST -Z,Rd
1760                            case 0x000a:    // ST -Y,Rd
15341761                                //output += sprintf( output, "ST      -Y , R%d", RD5(op) );
1535                                unimplemented_opcode(cpustate, op);
1762                                unimplemented_opcode(op);
15361763                                break;
15371764                            case 0x000c:    // ST X,Rd
1538                                WRITE_IO_8(cpustate, XREG, cpustate->r[RD5(op)]);
1765                                io_write8(XREG, m_r[RD5(op)]);
15391766                                break;
15401767                            case 0x000d:    // ST X+,Rd
15411768                                pd = XREG;
1542                                WRITE_IO_8(cpustate, pd, cpustate->r[RD5(op)]);
1769                                io_write8(pd, m_r[RD5(op)]);
15431770                                pd++;
1544                                cpustate->r[27] = (pd >> 8) & 0x00ff;
1545                                cpustate->r[26] = pd & 0x00ff;
1771                                m_r[27] = (pd >> 8) & 0x00ff;
1772                                m_r[26] = pd & 0x00ff;
15461773                                opcycles = 2;
15471774                                break;
15481775                            case 0x000e:    // ST -X,Rd
15491776                                //output += sprintf( output, "ST      -X , R%d", RD5(op) );
1550                                unimplemented_opcode(cpustate, op);
1777                                unimplemented_opcode(op);
15511778                                break;
15521779                            case 0x000f:    // PUSH Rd
1553                                PUSH(cpustate, cpustate->r[RD5(op)]);
1780                                push(m_r[RD5(op)]);
15541781                                opcycles = 2;
15551782                                break;
15561783                            default:
1557                                unimplemented_opcode(cpustate, op);
1784                                unimplemented_opcode(op);
15581785                                //output += sprintf( output, "Undefined (%04x)", op );
15591786                                break;
15601787                        }
r18789r18790
15631790                        switch(op & 0x000f)
15641791                        {
15651792                            case 0x0000:    // COM Rd
1566                                rd = cpustate->r[RD5(op)];
1793                                rd = m_r[RD5(op)];
15671794                                res = ~rd;
15681795                                SREG_W(AVR8_SREG_C, 1);
15691796                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
15701797                                SREG_W(AVR8_SREG_N, BIT(res,7));
15711798                                SREG_W(AVR8_SREG_V, 0);
15721799                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1573                                cpustate->r[RD5(op)] = res;
1800                                m_r[RD5(op)] = res;
15741801                                break;
15751802                            case 0x0001:    // NEG Rd
1576                                rd = cpustate->r[RD5(op)];
1803                                rd = m_r[RD5(op)];
15771804                                res = 0 - rd;
15781805                                SREG_W(AVR8_SREG_C, (res == 0) ? 0 : 1);
15791806                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
r18789r18790
15811808                                SREG_W(AVR8_SREG_V, (res == 0x80) ? 1 : 0);
15821809                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
15831810                                SREG_W(AVR8_SREG_H, BIT(res,3) | BIT(rd,3));
1584                                cpustate->r[RD5(op)] = res;
1811                                m_r[RD5(op)] = res;
15851812                                break;
15861813                            case 0x0002:    // SWAP Rd
1587                               rd = cpustate->r[RD5(op)];
1588                               cpustate->r[RD5(op)] = (rd >> 4) | (rd << 4);
1814                               rd = m_r[RD5(op)];
1815                               m_r[RD5(op)] = (rd >> 4) | (rd << 4);
15891816                                break;
15901817                            case 0x0003:    // INC Rd
1591                                rd = cpustate->r[RD5(op)];
1818                                rd = m_r[RD5(op)];
15921819                                res = rd + 1;
15931820                                SREG_W(AVR8_SREG_V, (rd == 0x7f) ? 1 : 0);
15941821                                SREG_W(AVR8_SREG_N, BIT(res,7));
15951822                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
15961823                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
1597                                cpustate->r[RD5(op)] = res;
1824                                m_r[RD5(op)] = res;
15981825                                break;
15991826                            case 0x0005:    // ASR Rd
1600                                rd = cpustate->r[RD5(op)];
1827                                rd = m_r[RD5(op)];
16011828                                res = (rd & 0x80) | (rd >> 1);
16021829                                SREG_W(AVR8_SREG_C, BIT(rd,0));
16031830                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
16041831                                SREG_W(AVR8_SREG_N, BIT(rd,7));
16051832                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
16061833                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1607                                cpustate->r[RD5(op)] = res;
1834                                m_r[RD5(op)] = res;
16081835                                break;
16091836                            case 0x0006:    // LSR Rd
1610                                rd = cpustate->r[RD5(op)];
1837                                rd = m_r[RD5(op)];
16111838                                res = rd >> 1;
16121839                                SREG_W(AVR8_SREG_C, BIT(rd,0));
16131840                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 :0);
16141841                                SREG_W(AVR8_SREG_N, 0);
16151842                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
16161843                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1617                                cpustate->r[RD5(op)] = res;
1844                                m_r[RD5(op)] = res;
16181845                                break;
16191846                            case 0x0007:    // ROR Rd
1620                               rd = cpustate->r[RD5(op)];
1847                               rd = m_r[RD5(op)];
16211848                               res = rd >> 1;
16221849                               res |= (SREG_R(AVR8_SREG_C) << 7);
16231850                                SREG_W(AVR8_SREG_C, BIT(rd,0));
r18789r18790
16251852                                SREG_W(AVR8_SREG_N, BIT(res,7));
16261853                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
16271854                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1628                                cpustate->r[RD5(op)] = res;
1855                                m_r[RD5(op)] = res;
16291856                                break;
16301857                            case 0x0008:
16311858                                switch(op & 0x00f0)
r18789r18790
16561883                                switch(op & 0x00f0)
16571884                                {
16581885                                    case 0x0000:    // IJMP
1659                                        cpustate->pc = ZREG - 1;
1886                                        m_pc = ZREG - 1;
16601887                                        opcycles = 2;
16611888                                        break;
16621889                                    case 0x0010:    // EIJMP
16631890                                        //output += sprintf( output, "EIJMP" );
1664                                        unimplemented_opcode(cpustate, op);
1891                                        unimplemented_opcode(op);
16651892                                        break;
16661893                                    default:
16671894                                        //output += sprintf( output, "Undefined (%04x)", op );
1668                                        unimplemented_opcode(cpustate, op);
1895                                        unimplemented_opcode(op);
16691896                                        break;
16701897                                }
16711898                                break;
16721899                            case 0x000a:    // DEC Rd
1673                                rd = cpustate->r[RD5(op)];
1900                                rd = m_r[RD5(op)];
16741901                                res = rd - 1;
16751902                                SREG_W(AVR8_SREG_V, (rd == 0x7f) ? 1 : 0);
16761903                                SREG_W(AVR8_SREG_N, BIT(res,7));
16771904                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
16781905                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
1679                                cpustate->r[RD5(op)] = res;
1906                                m_r[RD5(op)] = res;
16801907                                break;
16811908                            case 0x000c:
16821909                            case 0x000d:    // JMP k
16831910                        offs = KCONST22(op) << 16;
1684                                cpustate->pc++;
1685                                offs |= READ_PRG_16(cpustate, cpustate->pc);
1686                                cpustate->pc = offs;
1687                        cpustate->pc--;
1911                                m_pc++;
1912                                offs |= program_read16(m_pc);
1913                                m_pc = offs;
1914                        m_pc--;
16881915                        opcycles = 3;
16891916                                break;
16901917                            case 0x000e:    // CALL k
16911918                            case 0x000f:
1692                        PUSH(cpustate, ((cpustate->pc + 1) >> 8) & 0x00ff);
1693                        PUSH(cpustate, (cpustate->pc + 1) & 0x00ff);
1919                        push(((m_pc + 1) >> 8) & 0x00ff);
1920                        push((m_pc + 1) & 0x00ff);
16941921                        offs = KCONST22(op) << 16;
1695                                cpustate->pc++;
1696                                offs |= READ_PRG_16(cpustate, cpustate->pc);
1697                                cpustate->pc = offs;
1698                        cpustate->pc--;
1922                                m_pc++;
1923                                offs |= program_read16(m_pc);
1924                                m_pc = offs;
1925                        m_pc--;
16991926                        opcycles = 4;
17001927                                break;
17011928                            default:
1702                                unimplemented_opcode(cpustate, op);
1929                                unimplemented_opcode(op);
17031930                                //output += sprintf( output, "Undefined (%04x)", op );
17041931                                break;
17051932                        }
r18789r18790
17081935                        switch(op & 0x000f)
17091936                        {
17101937                            case 0x0000:    // COM Rd
1711                                rd = cpustate->r[RD5(op)];
1938                                rd = m_r[RD5(op)];
17121939                                res = ~rd;
17131940                                SREG_W(AVR8_SREG_C, 1);
17141941                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
17151942                                SREG_W(AVR8_SREG_N, BIT(res,7));
17161943                                SREG_W(AVR8_SREG_V, 0);
17171944                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1718                                cpustate->r[RD5(op)] = res;
1945                                m_r[RD5(op)] = res;
17191946                                break;
17201947                            case 0x0001:    // NEG Rd
1721                                rd = cpustate->r[RD5(op)];
1948                                rd = m_r[RD5(op)];
17221949                                res = 0 - rd;
17231950                                SREG_W(AVR8_SREG_C, (res == 0) ? 0 : 1);
17241951                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
r18789r18790
17261953                                SREG_W(AVR8_SREG_V, (res == 0x80) ? 1 : 0);
17271954                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
17281955                                SREG_W(AVR8_SREG_H, BIT(res,3) | BIT(rd,3));
1729                                cpustate->r[RD5(op)] = res;
1956                                m_r[RD5(op)] = res;
17301957                                break;
17311958                            case 0x0002:    // SWAP Rd
1732                               rd = cpustate->r[RD5(op)];
1733                               cpustate->r[RD5(op)] = (rd >> 4) | (rd << 4);
1959                               rd = m_r[RD5(op)];
1960                               m_r[RD5(op)] = (rd >> 4) | (rd << 4);
17341961                                break;
17351962                            case 0x0003:    // INC Rd
1736                                rd = cpustate->r[RD5(op)];
1963                                rd = m_r[RD5(op)];
17371964                                res = rd + 1;
17381965                                SREG_W(AVR8_SREG_V, (rd == 0x7f) ? 1 : 0);
17391966                                SREG_W(AVR8_SREG_N, BIT(res,7));
17401967                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
17411968                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
1742                                cpustate->r[RD5(op)] = res;
1969                                m_r[RD5(op)] = res;
17431970                                break;
17441971                            case 0x0005:    // ASR Rd
1745                                rd = cpustate->r[RD5(op)];
1972                                rd = m_r[RD5(op)];
17461973                                res = (rd & 0x80) | (rd >> 1);
17471974                                SREG_W(AVR8_SREG_C, BIT(rd,0));
17481975                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
17491976                                SREG_W(AVR8_SREG_N, BIT(rd,7));
17501977                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
17511978                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1752                                cpustate->r[RD5(op)] = res;
1979                                m_r[RD5(op)] = res;
17531980                                break;
17541981                            case 0x0006:    // LSR Rd
1755                                rd = cpustate->r[RD5(op)];
1982                                rd = m_r[RD5(op)];
17561983                                res = rd >> 1;
17571984                                SREG_W(AVR8_SREG_C, BIT(rd,0));
17581985                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 :0);
17591986                                SREG_W(AVR8_SREG_N, 0);
17601987                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
17611988                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1762                                cpustate->r[RD5(op)] = res;
1989                                m_r[RD5(op)] = res;
17631990                                break;
17641991                            case 0x0007:    // ROR Rd
1765                               rd = cpustate->r[RD5(op)];
1992                               rd = m_r[RD5(op)];
17661993                               res = rd >> 1;
17671994                               res |= (SREG_R(AVR8_SREG_C) << 7);
17681995                                SREG_W(AVR8_SREG_C, BIT(rd,0));
r18789r18790
17701997                                SREG_W(AVR8_SREG_N, BIT(res,7));
17711998                                SREG_W(AVR8_SREG_V, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_C));
17721999                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
1773                                cpustate->r[RD5(op)] = res;
2000                                m_r[RD5(op)] = res;
17742001                                break;
17752002                            case 0x0008:
17762003                                switch(op & 0x00f0)
17772004                                {
17782005                                    case 0x0000:    // RET
1779                                        cpustate->pc = POP(cpustate);
1780                                        cpustate->pc |= POP(cpustate) << 8;
1781                                        cpustate->pc--;
2006                                        m_pc = pop();
2007                                        m_pc |= pop() << 8;
2008                                        m_pc--;
17822009                                        opcycles = 4;
17832010                                        break;
17842011                                    case 0x0010:    // RETI
1785                                        cpustate->pc = POP(cpustate);
1786                                        cpustate->pc |= POP(cpustate) << 8;
1787                                        //printf("Pop: %04x\n", cpustate->pc);
1788                                        cpustate->pc--;
2012                                        m_pc = pop();
2013                                        m_pc |= pop() << 8;
2014                                        m_pc--;
17892015                                        SREG_W(AVR8_SREG_I, 1);
1790                                        /*if (cpustate->interrupt_pending)
1791                                        {
1792                                            avr8_poll_interrupt(cpustate);
1793                                            cpustate->interrupt_pending = false;
1794                                        }*/
17952016                                        opcycles = 4;
17962017                                        break;
17972018                                    case 0x0080:    // SLEEP
17982019                                        //output += sprintf( output, "SLEEP" );
1799                                        unimplemented_opcode(cpustate, op);
2020                                        unimplemented_opcode(op);
18002021                                        break;
18012022                                    case 0x0090:    // BREAK
18022023                                        //output += sprintf( output, "BREAK" );
1803                                        unimplemented_opcode(cpustate, op);
2024                                        unimplemented_opcode(op);
18042025                                        break;
18052026                                    case 0x00a0:    // WDR
18062027                                        //output += sprintf( output, "WDR" );
1807                                        unimplemented_opcode(cpustate, op);
2028                                        unimplemented_opcode(op);
18082029                                        break;
18092030                                    case 0x00c0:    // LPM
1810                                        cpustate->r[0] = READ_PRG_8(cpustate, ZREG);
2031                                        m_r[0] = program_read8(ZREG);
18112032                                        opcycles = 3;
18122033                                        break;
18132034                                    case 0x00d0:    // ELPM
18142035                                        //output += sprintf( output, "ELPM" );
1815                                        unimplemented_opcode(cpustate, op);
2036                                        unimplemented_opcode(op);
18162037                                        break;
18172038                                    case 0x00e0:    // SPM
18182039                                        //output += sprintf( output, "SPM" );
1819                                        unimplemented_opcode(cpustate, op);
2040                                        unimplemented_opcode(op);
18202041                                        break;
18212042                                    case 0x00f0:    // SPM Z+
18222043                                        //output += sprintf( output, "SPM     Z+" );
1823                                        unimplemented_opcode(cpustate, op);
2044                                        unimplemented_opcode(op);
18242045                                        break;
18252046                                    default:
1826                                        unimplemented_opcode(cpustate, op);
2047                                        unimplemented_opcode(op);
18272048                                        //output += sprintf( output, "Undefined (%04x)", op );
18282049                                        break;
18292050                                }
r18789r18790
18332054                                {
18342055                                    case 0x0000:    // ICALL
18352056                                        //output += sprintf( output, "ICALL" );
1836                                        unimplemented_opcode(cpustate, op);
2057                                        unimplemented_opcode(op);
18372058                                        break;
18382059                                    case 0x0010:    // EICALL
18392060                                        //output += sprintf( output, "EICALL" );
1840                                        unimplemented_opcode(cpustate, op);
2061                                        unimplemented_opcode(op);
18412062                                        break;
18422063                                    default:
1843                                        unimplemented_opcode(cpustate, op);
2064                                        unimplemented_opcode(op);
18442065                                        //output += sprintf( output, "Undefined (%04x)", op );
18452066                                        break;
18462067                                }
18472068                                break;
18482069                            case 0x000a:    // DEC Rd
1849                                rd = cpustate->r[RD5(op)];
2070                                rd = m_r[RD5(op)];
18502071                                res = rd - 1;
18512072                                SREG_W(AVR8_SREG_V, (rd == 0x7f) ? 1 : 0);
18522073                                SREG_W(AVR8_SREG_N, BIT(res,7));
18532074                                SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
18542075                                SREG_W(AVR8_SREG_Z, (res == 0) ? 1 : 0);
1855                                cpustate->r[RD5(op)] = res;
2076                                m_r[RD5(op)] = res;
18562077                                break;
18572078                            case 0x000c:
18582079                            case 0x000d:    // JMP k
r18789r18790
18612082                                //op <<= 8;
18622083                                //op |= oprom[pos++];
18632084                                //output += sprintf( output, "JMP     0x%06x", KCONST22(op) );
1864                                unimplemented_opcode(cpustate, op);
2085                                unimplemented_opcode(op);
18652086                                break;
18662087                            case 0x000e:
18672088                            case 0x000f:    // CALL k
r18789r18790
18702091                                //op <<= 8;
18712092                                //op |= oprom[pos++];
18722093                                //output += sprintf( output, "CALL    0x%06x", KCONST22(op) );
1873                                unimplemented_opcode(cpustate, op);
2094                                unimplemented_opcode(op);
18742095                                break;
18752096                        }
18762097                        break;
18772098                    case 0x0600:    // ADIW Rd+1:Rd,K
1878                        rd = cpustate->r[24 + (DCONST(op) << 1)];
1879                        rr = cpustate->r[25 + (DCONST(op) << 1)];
2099                        rd = m_r[24 + (DCONST(op) << 1)];
2100                        rr = m_r[25 + (DCONST(op) << 1)];
18802101                        pd = rd;
18812102                        pd |= rr << 8;
18822103                        pd += KCONST6(op);
r18789r18790
18852106                        SREG_W(AVR8_SREG_S, SREG_R(AVR8_SREG_N) ^ SREG_R(AVR8_SREG_V));
18862107                        SREG_W(AVR8_SREG_Z, (pd == 0) ? 1 : 0);
18872108                        SREG_W(AVR8_SREG_C, NOT(BIT(pd,15)) & BIT(rr,7));
1888                        cpustate->r[24 + (DCONST(op) << 1)] = pd & 0x00ff;
1889                        cpustate->r[25 + (DCONST(op) << 1)] = (pd >> 8) & 0x00ff;
2109                        m_r[24 + (DCONST(op) << 1)] = pd & 0x00ff;
2110                        m_r[25 + (DCONST(op) << 1)] = (pd >> 8) & 0x00ff;
18902111                        opcycles = 2;
18912112                        break;
18922113                    case 0x0700:    // SBIW Rd+1:Rd,K
18932114                        //output += sprintf( output, "SBIW    R%d:R%d, 0x%02x", 24+(RD2(op) << 1)+1, 24+(RD2(op) << 1), KCONST6(op) );
1894                        unimplemented_opcode(cpustate, op);
2115                        unimplemented_opcode(op);
18952116                        break;
18962117                    case 0x0800:    // CBI A,b
18972118                        //output += sprintf( output, "CBI     0x%02x, %d", ACONST5(op), RR3(op) );
1898                        WRITE_IO_8(cpustate, 32 + ACONST5(op), READ_IO_8(cpustate, 32 + ACONST5(op)) &~ (1 << RR3(op)));
2119                        io_write8(32 + ACONST5(op), io_read8(32 + ACONST5(op)) &~ (1 << RR3(op)));
18992120                        opcycles = 2;
19002121                        break;
19012122                    case 0x0900:    // SBIC A,b
1902                      if(NOT(BIT(READ_IO_8(cpustate, 32 + ACONST5(op)), RR3(op))))
2123                      if(NOT(BIT(io_read8(32 + ACONST5(op)), RR3(op))))
19032124                      {
1904                            op = (UINT32)READ_PRG_16(cpustate, cpustate->pc + 1);
1905                     opcycles = avr8_is_long_opcode(op) ? 3 : 2;
1906                            cpustate->pc += avr8_is_long_opcode(op) ? 2 : 1;
2125                            op = (UINT32)program_read16(m_pc + 1);
2126                     opcycles = is_long_opcode(op) ? 3 : 2;
2127                            m_pc += is_long_opcode(op) ? 2 : 1;
19072128                  }
19082129                        break;
19092130                    case 0x0a00:    // SBI A,b
1910                        WRITE_IO_8(cpustate, 32 + ACONST5(op), READ_IO_8(cpustate, 32 + ACONST5(op)) | (1 << RR3(op)));
2131                        io_write8(32 + ACONST5(op), io_read8(32 + ACONST5(op)) | (1 << RR3(op)));
19112132                        opcycles = 2;
19122133                        break;
19132134                    case 0x0b00:    // SBIS A,b
1914                      if(BIT(READ_IO_8(cpustate, 32 + ACONST5(op)), RR3(op)))
2135                      if(BIT(io_read8(32 + ACONST5(op)), RR3(op)))
19152136                      {
1916                            op = (UINT32)READ_PRG_16(cpustate, cpustate->pc + 1);
1917                     opcycles = avr8_is_long_opcode(op) ? 3 : 2;
1918                            cpustate->pc += avr8_is_long_opcode(op) ? 2 : 1;
2137                            op = (UINT32)program_read16(m_pc + 1);
2138                     opcycles = is_long_opcode(op) ? 3 : 2;
2139                            m_pc += is_long_opcode(op) ? 2 : 1;
19192140                  }
19202141                        break;
19212142                    case 0x0c00:
19222143                    case 0x0d00:
19232144                    case 0x0e00:
19242145                    case 0x0f00:    // MUL Rd,Rr
1925                        sd = (UINT8)cpustate->r[RD5(op)] * (UINT8)cpustate->r[RR5(op)];
1926                        cpustate->r[1] = (sd >> 8) & 0x00ff;
1927                        cpustate->r[0] = sd & 0x00ff;
2146                        sd = (UINT8)m_r[RD5(op)] * (UINT8)m_r[RR5(op)];
2147                        m_r[1] = (sd >> 8) & 0x00ff;
2148                        m_r[0] = sd & 0x00ff;
19282149                        SREG_W(AVR8_SREG_C, (sd & 0x8000) ? 1 : 0);
19292150                        SREG_W(AVR8_SREG_Z, (sd == 0) ? 1 : 0);
19302151                        opcycles = 2;
r18789r18790
19342155            case 0xb000:
19352156                if(op & 0x0800) // OUT A,Rr
19362157                {
1937                    WRITE_IO_8(cpustate, 32 + ACONST6(op), cpustate->r[RD5(op)]);
2158                    io_write8(32 + ACONST6(op), m_r[RD5(op)]);
19382159                }
19392160                else            // IN Rd,A
19402161                {
1941                    cpustate->r[RD5(op)] = READ_IO_8(cpustate, 0x20 + ACONST6(op));
2162                    m_r[RD5(op)] = io_read8(0x20 + ACONST6(op));
19422163                }
19432164                break;
19442165            case 0xc000:    // RJMP k
19452166                offs = (INT32)((op & 0x0800) ? ((op & 0x0fff) | 0xfffff000) : (op & 0x0fff));
1946                cpustate->pc += offs;
2167                m_pc += offs;
19472168                opcycles = 2;
19482169                break;
19492170            case 0xd000:    // RCALL k
19502171                offs = (INT32)((op & 0x0800) ? ((op & 0x0fff) | 0xfffff000) : (op & 0x0fff));
1951                PUSH(cpustate, ((cpustate->pc + 1) >> 8) & 0x00ff);
1952                PUSH(cpustate, (cpustate->pc + 1) & 0x00ff);
1953                cpustate->pc += offs;
2172                push(((m_pc + 1) >> 8) & 0x00ff);
2173                push((m_pc + 1) & 0x00ff);
2174                m_pc += offs;
19542175                opcycles = 3;
19552176                break;
19562177            case 0xe000:    // LDI Rd,K
1957                cpustate->r[16 + RD4(op)] = KCONST8(op);
2178                m_r[16 + RD4(op)] = KCONST8(op);
19582179                break;
19592180            case 0xf000:
19602181                switch(op & 0x0c00)
r18789r18790
19672188                            {
19682189                                offs |= 0xffffff80;
19692190                            }
1970                            cpustate->pc += offs;
2191                            m_pc += offs;
19712192                            opcycles = 2;
19722193                        }
19732194                        break;
r18789r18790
19792200                            {
19802201                                offs |= 0xffffff80;
19812202                            }
1982                            cpustate->pc += offs;
2203                            m_pc += offs;
19832204                            opcycles = 2;
19842205                        }
19852206                        break;
19862207                    case 0x0800:
19872208                        if(op & 0x0200) // BST Rd, b
19882209                        {
1989                            SREG_W(AVR8_SREG_T, (BIT(cpustate->r[RD5(op)], RR3(op))) ? 1 : 0);
2210                            SREG_W(AVR8_SREG_T, (BIT(m_r[RD5(op)], RR3(op))) ? 1 : 0);
19902211                        }
19912212                        else            // BLD Rd, b
19922213                        {
19932214                            if(SREG_R(AVR8_SREG_T))
19942215                            {
1995                                cpustate->r[RD5(op)] |= (1 << RR3(op));
2216                                m_r[RD5(op)] |= (1 << RR3(op));
19962217                            }
19972218                            else
19982219                            {
1999                                cpustate->r[RD5(op)] &= ~(1 << RR3(op));
2220                                m_r[RD5(op)] &= ~(1 << RR3(op));
20002221                            }
20012222                        }
20022223                        break;
20032224                    case 0x0c00:
20042225                        if(op & 0x0200) // SBRS Rd, b
20052226                        {
2006                            if(BIT(cpustate->r[RD5(op)], RR3(op)))
2227                            if(BIT(m_r[RD5(op)], RR3(op)))
20072228                            {
2008                                op = (UINT32)READ_PRG_16(cpustate, cpustate->pc++);
2009                                cpustate->pc += avr8_is_long_opcode(op) ? 1 : 0;
2010                                opcycles = avr8_is_long_opcode(op) ? 3 : 2;
2229                                op = (UINT32)program_read16(m_pc++);
2230                                m_pc += is_long_opcode(op) ? 1 : 0;
2231                                opcycles = is_long_opcode(op) ? 3 : 2;
20112232                            }
20122233                        }
20132234                        else            // SBRC Rd, b
20142235                        {
2015                            if(NOT(BIT(cpustate->r[RD5(op)], RR3(op))))
2236                            if(NOT(BIT(m_r[RD5(op)], RR3(op))))
20162237                            {
2017                                op = (UINT32)READ_PRG_16(cpustate, cpustate->pc++);
2018                                cpustate->pc += avr8_is_long_opcode(op) ? 1 : 0;
2019                                opcycles = avr8_is_long_opcode(op) ? 3 : 2;
2238                                op = (UINT32)program_read16(m_pc++);
2239                                m_pc += is_long_opcode(op) ? 1 : 0;
2240                                opcycles = is_long_opcode(op) ? 3 : 2;
20202241                            }
20212242                        }
20222243                        break;
r18789r18790
20242245                break;
20252246        }
20262247
2027        cpustate->pc++;
2248        m_pc++;
20282249
2029        cpustate->icount -= opcycles;
2250        m_debugger_pc = m_pc << 1;
20302251
2031      cpustate->elapsed_cycles += opcycles;
2252        m_icount -= opcycles;
20322253
2033      avr8_timer_tick(cpustate, opcycles);
2034    }
2035}
2254      m_elapsed_cycles += opcycles;
20362255
2037/*****************************************************************************/
2038
2039static CPU_SET_INFO( avr8 )
2040{
2041    avr8_state *cpustate = get_safe_token(device);
2042
2043    switch (state)
2044    {
2045        /* interrupt lines/exceptions */
2046        case CPUINFO_INT_INPUT_STATE + AVR8_INT_RESET:          avr8_set_irq_line(cpustate, AVR8_INT_RESET, info->i);       break;
2047        case CPUINFO_INT_INPUT_STATE + AVR8_INT_INT0:           avr8_set_irq_line(cpustate, AVR8_INT_INT0, info->i);        break;
2048        case CPUINFO_INT_INPUT_STATE + AVR8_INT_INT1:           avr8_set_irq_line(cpustate, AVR8_INT_INT1, info->i);        break;
2049        case CPUINFO_INT_INPUT_STATE + AVR8_INT_PCINT0:         avr8_set_irq_line(cpustate, AVR8_INT_PCINT0, info->i);      break;
2050        case CPUINFO_INT_INPUT_STATE + AVR8_INT_PCINT1:         avr8_set_irq_line(cpustate, AVR8_INT_PCINT1, info->i);      break;
2051        case CPUINFO_INT_INPUT_STATE + AVR8_INT_PCINT2:         avr8_set_irq_line(cpustate, AVR8_INT_PCINT2, info->i);      break;
2052        case CPUINFO_INT_INPUT_STATE + AVR8_INT_WDT:            avr8_set_irq_line(cpustate, AVR8_INT_WDT, info->i);         break;
2053        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T2COMPA:        avr8_set_irq_line(cpustate, AVR8_INT_T2COMPA, info->i);     break;
2054        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T2COMPB:        avr8_set_irq_line(cpustate, AVR8_INT_T2COMPB, info->i);     break;
2055        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T2OVF:          avr8_set_irq_line(cpustate, AVR8_INT_T2OVF, info->i);       break;
2056        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T1CAPT:         avr8_set_irq_line(cpustate, AVR8_INT_T1CAPT, info->i);      break;
2057        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T1COMPA:        avr8_set_irq_line(cpustate, AVR8_INT_T1COMPA, info->i);     break;
2058        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T1COMPB:        avr8_set_irq_line(cpustate, AVR8_INT_T1COMPB, info->i);     break;
2059        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T1OVF:          avr8_set_irq_line(cpustate, AVR8_INT_T1OVF, info->i);       break;
2060        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T0COMPA:        avr8_set_irq_line(cpustate, AVR8_INT_T0COMPA, info->i);     break;
2061        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T0COMPB:        avr8_set_irq_line(cpustate, AVR8_INT_T0COMPB, info->i);     break;
2062        case CPUINFO_INT_INPUT_STATE + AVR8_INT_T0OVF:          avr8_set_irq_line(cpustate, AVR8_INT_T0OVF, info->i);       break;
2063        case CPUINFO_INT_INPUT_STATE + AVR8_INT_SPI_STC:        avr8_set_irq_line(cpustate, AVR8_INT_SPI_STC, info->i);     break;
2064        case CPUINFO_INT_INPUT_STATE + AVR8_INT_USART_RX:       avr8_set_irq_line(cpustate, AVR8_INT_USART_RX, info->i);    break;
2065        case CPUINFO_INT_INPUT_STATE + AVR8_INT_USART_UDRE:     avr8_set_irq_line(cpustate, AVR8_INT_USART_UDRE, info->i);  break;
2066        case CPUINFO_INT_INPUT_STATE + AVR8_INT_USART_TX:       avr8_set_irq_line(cpustate, AVR8_INT_USART_TX, info->i);    break;
2067        case CPUINFO_INT_INPUT_STATE + AVR8_INT_ADC:            avr8_set_irq_line(cpustate, AVR8_INT_ADC, info->i);         break;
2068        case CPUINFO_INT_INPUT_STATE + AVR8_INT_EE_RDY:         avr8_set_irq_line(cpustate, AVR8_INT_EE_RDY, info->i);      break;
2069        case CPUINFO_INT_INPUT_STATE + AVR8_INT_ANALOG_COMP:    avr8_set_irq_line(cpustate, AVR8_INT_ANALOG_COMP, info->i); break;
2070        case CPUINFO_INT_INPUT_STATE + AVR8_INT_TWI:            avr8_set_irq_line(cpustate, AVR8_INT_TWI, info->i);         break;
2071        case CPUINFO_INT_INPUT_STATE + AVR8_INT_SPM_RDY:        avr8_set_irq_line(cpustate, AVR8_INT_SPM_RDY, info->i);     break;
2072
2073        /* --- the following bits of info are set as 64-bit signed integers --- */
2074        case CPUINFO_INT_PC:    /* intentional fallthrough */
2075        case CPUINFO_INT_REGISTER + AVR8_PC:            cpustate->pc = info->i;                 break;
2076        case CPUINFO_INT_REGISTER + AVR8_SREG:          cpustate->status = info->i;          break;
2077        case CPUINFO_INT_REGISTER + AVR8_R0:            cpustate->r[ 0] = info->i;              break;
2078        case CPUINFO_INT_REGISTER + AVR8_R1:            cpustate->r[ 1] = info->i;              break;
2079        case CPUINFO_INT_REGISTER + AVR8_R2:            cpustate->r[ 2] = info->i;              break;
2080        case CPUINFO_INT_REGISTER + AVR8_R3:            cpustate->r[ 3] = info->i;              break;
2081        case CPUINFO_INT_REGISTER + AVR8_R4:            cpustate->r[ 4] = info->i;              break;
2082        case CPUINFO_INT_REGISTER + AVR8_R5:            cpustate->r[ 5] = info->i;              break;
2083        case CPUINFO_INT_REGISTER + AVR8_R6:            cpustate->r[ 6] = info->i;              break;
2084        case CPUINFO_INT_REGISTER + AVR8_R7:            cpustate->r[ 7] = info->i;              break;
2085        case CPUINFO_INT_REGISTER + AVR8_R8:            cpustate->r[ 8] = info->i;              break;
2086        case CPUINFO_INT_REGISTER + AVR8_R9:            cpustate->r[ 9] = info->i;              break;
2087        case CPUINFO_INT_REGISTER + AVR8_R10:           cpustate->r[10] = info->i;              break;
2088        case CPUINFO_INT_REGISTER + AVR8_R11:           cpustate->r[11] = info->i;              break;
2089        case CPUINFO_INT_REGISTER + AVR8_R12:           cpustate->r[12] = info->i;              break;
2090        case CPUINFO_INT_REGISTER + AVR8_R13:           cpustate->r[13] = info->i;              break;
2091        case CPUINFO_INT_REGISTER + AVR8_R14:           cpustate->r[14] = info->i;              break;
2092        case CPUINFO_INT_REGISTER + AVR8_R15:           cpustate->r[15] = info->i;              break;
2093        case CPUINFO_INT_REGISTER + AVR8_R16:           cpustate->r[16] = info->i;              break;
2094        case CPUINFO_INT_REGISTER + AVR8_R17:           cpustate->r[17] = info->i;              break;
2095        case CPUINFO_INT_REGISTER + AVR8_R18:           cpustate->r[18] = info->i;              break;
2096        case CPUINFO_INT_REGISTER + AVR8_R19:           cpustate->r[19] = info->i;              break;
2097        case CPUINFO_INT_REGISTER + AVR8_R20:           cpustate->r[20] = info->i;              break;
2098        case CPUINFO_INT_REGISTER + AVR8_R21:           cpustate->r[21] = info->i;              break;
2099        case CPUINFO_INT_REGISTER + AVR8_R22:           cpustate->r[22] = info->i;              break;
2100        case CPUINFO_INT_REGISTER + AVR8_R23:           cpustate->r[23] = info->i;              break;
2101        case CPUINFO_INT_REGISTER + AVR8_R24:           cpustate->r[24] = info->i;              break;
2102        case CPUINFO_INT_REGISTER + AVR8_R25:           cpustate->r[25] = info->i;              break;
2103        case CPUINFO_INT_REGISTER + AVR8_R26:           cpustate->r[26] = info->i;              break;
2104        case CPUINFO_INT_REGISTER + AVR8_R27:           cpustate->r[27] = info->i;              break;
2105        case CPUINFO_INT_REGISTER + AVR8_R28:           cpustate->r[28] = info->i;              break;
2106        case CPUINFO_INT_REGISTER + AVR8_R29:           cpustate->r[29] = info->i;              break;
2107        case CPUINFO_INT_REGISTER + AVR8_R30:           cpustate->r[30] = info->i;              break;
2108        case CPUINFO_INT_REGISTER + AVR8_R31:           cpustate->r[31] = info->i;              break;
2256      timer_tick(opcycles);
21092257    }
21102258}
2111
2112CPU_GET_INFO( avr8 )
2113{
2114    avr8_state *cpustate = (device != NULL && device->token() != NULL) ? get_safe_token(device) : NULL;
2115
2116    switch(state)
2117    {
2118        /* --- the following bits of info are returned as 64-bit signed integers --- */
2119        case CPUINFO_INT_CONTEXT_SIZE:          info->i = sizeof(avr8_state);   break;
2120        case CPUINFO_INT_INPUT_LINES:           info->i = 0;                    break;
2121        case CPUINFO_INT_DEFAULT_IRQ_VECTOR:    info->i = 0;                    break;
2122        case CPUINFO_INT_ENDIANNESS:            info->i = ENDIANNESS_LITTLE;    break;
2123        case CPUINFO_INT_CLOCK_MULTIPLIER:      info->i = 1;                    break;
2124        case CPUINFO_INT_CLOCK_DIVIDER:         info->i = 1;                    break;
2125        case CPUINFO_INT_MIN_INSTRUCTION_BYTES: info->i = 2;                    break;
2126        case CPUINFO_INT_MAX_INSTRUCTION_BYTES: info->i = 4;                    break;
2127        case CPUINFO_INT_MIN_CYCLES:            info->i = 1;                    break;
2128        case CPUINFO_INT_MAX_CYCLES:            info->i = 4;                    break;
2129
2130        case CPUINFO_INT_DATABUS_WIDTH + AS_PROGRAM: info->i = 8;                    break;
2131        case CPUINFO_INT_ADDRBUS_WIDTH + AS_PROGRAM: info->i = 22;                   break;
2132        case CPUINFO_INT_ADDRBUS_SHIFT + AS_PROGRAM: info->i = 0;                    break;
2133        case CPUINFO_INT_DATABUS_WIDTH + AS_DATA:    info->i = 0;                    break;
2134        case CPUINFO_INT_ADDRBUS_WIDTH + AS_DATA:    info->i = 0;                    break;
2135        case CPUINFO_INT_ADDRBUS_SHIFT + AS_DATA:    info->i = 0;                    break;
2136        case CPUINFO_INT_DATABUS_WIDTH + AS_IO:      info->i = 8;                    break;
2137        case CPUINFO_INT_ADDRBUS_WIDTH + AS_IO:      info->i = 11;                   break;
2138        case CPUINFO_INT_ADDRBUS_SHIFT + AS_IO:      info->i = 0;                    break;
2139
2140        case CPUINFO_INT_PC:    /* intentional fallthrough */
2141        case CPUINFO_INT_REGISTER + AVR8_PC:    info->i = cpustate->pc << 1;   break;
2142        case CPUINFO_INT_REGISTER + AVR8_SREG:  info->i = cpustate->status;    break;
2143
2144        /* --- the following bits of info are returned as pointers to data or functions --- */
2145        case CPUINFO_FCT_SET_INFO:              info->setinfo = CPU_SET_INFO_NAME(avr8);        break;
2146        case CPUINFO_FCT_INIT:                  info->init = CPU_INIT_NAME(avr8);               break;
2147        case CPUINFO_FCT_RESET:                 info->reset = CPU_RESET_NAME(avr8);             break;
2148        case CPUINFO_FCT_EXIT:                  info->exit = CPU_EXIT_NAME(avr8);               break;
2149        case CPUINFO_FCT_EXECUTE:               info->execute = CPU_EXECUTE_NAME(avr8);         break;
2150        case CPUINFO_FCT_BURN:                  info->burn = NULL;                              break;
2151        case CPUINFO_FCT_DISASSEMBLE:           info->disassemble = CPU_DISASSEMBLE_NAME(avr8); break;
2152        case CPUINFO_PTR_INSTRUCTION_COUNTER:   info->icount = &cpustate->icount;               break;
2153
2154        /* --- the following bits of info are returned as NULL-terminated strings --- */
2155        case CPUINFO_STR_NAME:                          strcpy(info->s, "Atmel 8-bit AVR");     break;
2156        case CPUINFO_STR_FAMILY:                   strcpy(info->s, "AVR8");                break;
2157        case CPUINFO_STR_VERSION:                  strcpy(info->s, "1.0");                 break;
2158        case CPUINFO_STR_SOURCE_FILE:                     strcpy(info->s, __FILE__);              break;
2159        case CPUINFO_STR_CREDITS:                  strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
2160
2161        case CPUINFO_STR_FLAGS:                         strcpy(info->s, " ");                   break;
2162
2163        case CPUINFO_STR_REGISTER + AVR8_SREG:          sprintf(info->s, "SREG: %c%c%c%c%c%c%c%c", (cpustate->status & 0x80) ? 'I' : '-', (cpustate->status & 0x40) ? 'T' : '-', (cpustate->status & 0x20) ? 'H' : '-', (cpustate->status & 0x10) ? 'S' : '-', (cpustate->status & 0x08) ? 'V' : '-', (cpustate->status & 0x04) ? 'N' : '-', (cpustate->status & 0x02) ? 'Z' : '-', (cpustate->status & 0x01) ? 'C' : '-'); break;
2164        case CPUINFO_STR_REGISTER + AVR8_R0:            sprintf(info->s, "R0:  %02x", cpustate->r[ 0] ); break;
2165        case CPUINFO_STR_REGISTER + AVR8_R1:            sprintf(info->s, "R1:  %02x", cpustate->r[ 1] ); break;
2166        case CPUINFO_STR_REGISTER + AVR8_R2:            sprintf(info->s, "R2:  %02x", cpustate->r[ 2] ); break;
2167        case CPUINFO_STR_REGISTER + AVR8_R3:            sprintf(info->s, "R3:  %02x", cpustate->r[ 3] ); break;
2168        case CPUINFO_STR_REGISTER + AVR8_R4:            sprintf(info->s, "R4:  %02x", cpustate->r[ 4] ); break;
2169        case CPUINFO_STR_REGISTER + AVR8_R5:            sprintf(info->s, "R5:  %02x", cpustate->r[ 5] ); break;
2170        case CPUINFO_STR_REGISTER + AVR8_R6:            sprintf(info->s, "R6:  %02x", cpustate->r[ 6] ); break;
2171        case CPUINFO_STR_REGISTER + AVR8_R7:            sprintf(info->s, "R7:  %02x", cpustate->r[ 7] ); break;
2172        case CPUINFO_STR_REGISTER + AVR8_R8:            sprintf(info->s, "R8:  %02x", cpustate->r[ 8] ); break;
2173        case CPUINFO_STR_REGISTER + AVR8_R9:            sprintf(info->s, "R9:  %02x", cpustate->r[ 9] ); break;
2174        case CPUINFO_STR_REGISTER + AVR8_R10:           sprintf(info->s, "R10: %02x", cpustate->r[10] ); break;
2175        case CPUINFO_STR_REGISTER + AVR8_R11:           sprintf(info->s, "R11: %02x", cpustate->r[11] ); break;
2176        case CPUINFO_STR_REGISTER + AVR8_R12:           sprintf(info->s, "R12: %02x", cpustate->r[12] ); break;
2177        case CPUINFO_STR_REGISTER + AVR8_R13:           sprintf(info->s, "R13: %02x", cpustate->r[13] ); break;
2178        case CPUINFO_STR_REGISTER + AVR8_R14:           sprintf(info->s, "R14: %02x", cpustate->r[14] ); break;
2179        case CPUINFO_STR_REGISTER + AVR8_R15:           sprintf(info->s, "R15: %02x", cpustate->r[15] ); break;
2180        case CPUINFO_STR_REGISTER + AVR8_R16:           sprintf(info->s, "R16: %02x", cpustate->r[16] ); break;
2181        case CPUINFO_STR_REGISTER + AVR8_R17:           sprintf(info->s, "R17: %02x", cpustate->r[17] ); break;
2182        case CPUINFO_STR_REGISTER + AVR8_R18:           sprintf(info->s, "R18: %02x", cpustate->r[18] ); break;
2183        case CPUINFO_STR_REGISTER + AVR8_R19:           sprintf(info->s, "R19: %02x", cpustate->r[19] ); break;
2184        case CPUINFO_STR_REGISTER + AVR8_R20:           sprintf(info->s, "R20: %02x", cpustate->r[20] ); break;
2185        case CPUINFO_STR_REGISTER + AVR8_R21:           sprintf(info->s, "R21: %02x", cpustate->r[21] ); break;
2186        case CPUINFO_STR_REGISTER + AVR8_R22:           sprintf(info->s, "R22: %02x", cpustate->r[22] ); break;
2187        case CPUINFO_STR_REGISTER + AVR8_R23:           sprintf(info->s, "R23: %02x", cpustate->r[23] ); break;
2188        case CPUINFO_STR_REGISTER + AVR8_R24:           sprintf(info->s, "R24: %02x", cpustate->r[24] ); break;
2189        case CPUINFO_STR_REGISTER + AVR8_R25:           sprintf(info->s, "R25: %02x", cpustate->r[25] ); break;
2190        case CPUINFO_STR_REGISTER + AVR8_R26:           sprintf(info->s, "R26: %02x", cpustate->r[26] ); break;
2191        case CPUINFO_STR_REGISTER + AVR8_R27:           sprintf(info->s, "R27: %02x", cpustate->r[27] ); break;
2192        case CPUINFO_STR_REGISTER + AVR8_R28:           sprintf(info->s, "R28: %02x", cpustate->r[28] ); break;
2193        case CPUINFO_STR_REGISTER + AVR8_R29:           sprintf(info->s, "R29: %02x", cpustate->r[29] ); break;
2194        case CPUINFO_STR_REGISTER + AVR8_R30:           sprintf(info->s, "R30: %02x", cpustate->r[30] ); break;
2195        case CPUINFO_STR_REGISTER + AVR8_R31:           sprintf(info->s, "R31: %02x", cpustate->r[31] ); break;
2196        case CPUINFO_STR_REGISTER + AVR8_X:             sprintf(info->s, "X: %04x", XREG ); break;
2197        case CPUINFO_STR_REGISTER + AVR8_Y:             sprintf(info->s, "Y: %04x", YREG ); break;
2198        case CPUINFO_STR_REGISTER + AVR8_Z:             sprintf(info->s, "Z: %04x", ZREG ); break;
2199        case CPUINFO_STR_REGISTER + AVR8_SP:            sprintf(info->s, "SP: %04x", SPREG ); break;
2200    }
2201}
2202
2203static CPU_INIT( atmega88 )
2204{
2205   CPU_INIT_CALL(avr8);
2206    avr8_state *cpustate = get_safe_token(device);
2207   cpustate->addr_mask = 0x0fff;
2208}
2209
2210static CPU_INIT( atmega644 )
2211{
2212   CPU_INIT_CALL(avr8);
2213    avr8_state *cpustate = get_safe_token(device);
2214   cpustate->addr_mask = 0xffff;
2215}
2216
2217CPU_GET_INFO( atmega88 )
2218{
2219   switch (state)
2220   {
2221      /* --- the following bits of info are returned as pointers to functions --- */
2222      case CPUINFO_FCT_INIT:                     info->init = CPU_INIT_NAME(atmega88);      break;
2223
2224      /* --- the following bits of info are returned as NULL-terminated strings --- */
2225      case CPUINFO_STR_NAME:                     strcpy(info->s, "ATmega88");            break;
2226
2227      default:                              CPU_GET_INFO_CALL(avr8); break;
2228   }
2229}
2230
2231CPU_GET_INFO( atmega644 )
2232{
2233   switch (state)
2234   {
2235      /* --- the following bits of info are returned as pointers to functions --- */
2236      case CPUINFO_FCT_INIT:                     info->init = CPU_INIT_NAME(atmega644);      break;
2237
2238      /* --- the following bits of info are returned as NULL-terminated strings --- */
2239      case CPUINFO_STR_NAME:                     strcpy(info->s, "ATmega644");            break;
2240
2241      default:                              CPU_GET_INFO_CALL(avr8); break;
2242   }
2243}
2244
2245DEFINE_LEGACY_CPU_DEVICE(ATMEGA88, atmega88);
2246DEFINE_LEGACY_CPU_DEVICE(ATMEGA644, atmega644);
trunk/src/emu/cpu/avr8/avr8.h
r18789r18790
11/*
22    Atmel 8-bit AVR simulator
33
4    (Skeleton)
4    - Notes -
5      Cycle counts are generally considered to be 100% accurate per-instruction, does not support mid-instruction
6      interrupts although no software has been countered yet that requires it. Evidence of cycle accuracy is given
7      in the form of the demoscene 'wild' demo, Craft, by [lft], which uses an ATmega88 to write video out a 6-bit
8      RGB DAC pixel-by-pixel, synchronously with the frame timing. Intentionally modifying the timing of any of
9      the existing opcodes has been shown to wildly corrupt the video output in Craft, so one can assume that the
10      existing timing is 100% correct.
511
6    Written by MooglyGuy
12     Unimplemented opcodes: CPSR, LD Z+, ST Z+, ST -Z/-Y/-X, ELPM, SPM, SPM Z+, EIJMP, SLEEP, BREAK, WDR, ICALL,
13                            EICALL, JMP, CALL, SBIW
14
15   - Changelist -
16     30 Oct. 2012
17     - Added FMUL, FMULS, FMULSU opcodes [MooglyGuy]
18     - Fixed incorrect flag calculation in ROR opcode [MooglyGuy]
19     - Fixed incorrect bit testing in SBIC/SBIS opcodes [MooglyGuy]
20
21     25 Oct. 2012
22     - Added MULS, ANDI, STI Z+, LD -Z, LD -Y, LD -X, LD Y+q, LD Z+q, SWAP, ASR, ROR and SBIS opcodes [MooglyGuy]
23     - Corrected cycle counts for LD and ST opcodes [MooglyGuy]
24     - Moved opcycles init into inner while loop, fixes 2-cycle and 3-cycle opcodes effectively forcing
25       all subsequent 1-cycle opcodes to be 2 or 3 cycles [MooglyGuy]
26     - Fixed register behavior in MULSU, LD -Z, and LD -Y opcodes [MooglyGuy]
27
28     18 Oct. 2012
29     - Added OR, SBCI, ORI, ST Y+, ADIQ opcodes [MooglyGuy]
30     - Fixed COM, NEG, LSR opcodes [MooglyGuy]
31
732*/
833
934#pragma once
r18789r18790
1136#ifndef __AVR8_H__
1237#define __AVR8_H__
1338
39//**************************************************************************
40//  INTERFACE CONFIGURATION MACROS
41//**************************************************************************
42
43#define MCFG_CPU_AVR8_CONFIG(_config) \
44   avr8_device::static_set_config(*device, _config); \
45
46//**************************************************************************
47//  TYPE DEFINITIONS
48//**************************************************************************
49
50class avr8_device;
51
52// ======================> avr8_config
53
54struct avr8_config
55{
56   void (*m_portb_changed)(avr8_device &device, UINT8 pins, UINT8 changed);
57   void (*m_portc_changed)(avr8_device &device, UINT8 pins, UINT8 changed);
58   void (*m_portd_changed)(avr8_device &device, UINT8 pins, UINT8 changed);
59};
60
61
62// ======================> avr8_device
63
1464// Used by core CPU interface
15struct avr8_state
65class avr8_device : public cpu_device,
66               public avr8_config
1667{
17    UINT32 pc;
68public:
69   // construction/destruction
70   avr8_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock, const device_type type, UINT32 address_mask);
1871
19    legacy_cpu_device *device;
20    address_space *program;
21    address_space *io;
72   // inline configuration helpers
73   static void static_set_config(device_t &device, const avr8_config &config);
2274
23    int icount;
24   UINT32 addr_mask;
75   // public interfaces
76   void update_interrupt(int source);
77   UINT64 get_elapsed_cycles()
78   {
79      return m_elapsed_cycles;
80   }
2581
26   UINT8 r[256];
27   UINT8 status;
82protected:
83   // device-level overrides
84   virtual void device_start();
85   virtual void device_reset();
2886
29    UINT8 timer0_top;
30   INT32 timer0_increment;
31   UINT16 timer0_prescale;
32   UINT16 timer0_prescale_count;
87   // device_execute_interface overrides
88   virtual UINT32 execute_min_cycles() const;
89   virtual UINT32 execute_max_cycles() const;
90   virtual UINT32 execute_input_lines() const;
91   virtual void execute_run();
92   virtual void execute_set_input(int inputnum, int state);
3393
34    UINT16 timer1_top;
35   INT32 timer1_increment;
36   UINT16 timer1_prescale;
37   UINT16 timer1_prescale_count;
94   // device_memory_interface overrides
95   virtual const address_space_config *memory_space_config(address_spacenum spacenum = AS_0) const;
3896
39    UINT8 timer2_top;
40   INT32 timer2_increment;
41   UINT16 timer2_prescale;
42   UINT16 timer2_prescale_count;
97   // device_disasm_interface overrides
98   virtual UINT32 disasm_min_opcode_bytes() const;
99   virtual UINT32 disasm_max_opcode_bytes() const;
100   virtual offs_t disasm_disassemble(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram, UINT32 options);
43101
44   UINT64 elapsed_cycles;
102   // device_state_interface overrides
103   virtual void state_string_export(const device_state_entry &entry, astring &string);
45104
46   bool interrupt_pending;
105   // address spaces
106   const address_space_config m_program_config;
107   const address_space_config m_io_config;
108
109   // CPU registers
110    UINT32 m_pc;
111    UINT32 m_debugger_pc;
112   UINT8 m_status;
113   UINT8 m_r[256];
114
115   // On-chip Device Registers
116    UINT8 m_timer0_top;
117   INT32 m_timer0_increment;
118   UINT16 m_timer0_prescale;
119   UINT16 m_timer0_prescale_count;
120
121    UINT16 m_timer1_top;
122   INT32 m_timer1_increment;
123   UINT16 m_timer1_prescale;
124   UINT16 m_timer1_prescale_count;
125
126    UINT8 m_timer2_top;
127   INT32 m_timer2_increment;
128   UINT16 m_timer2_prescale;
129   UINT16 m_timer2_prescale_count;
130
131    // internal stuff
132   UINT32 m_addr_mask;
133   bool m_interrupt_pending;
134
135   // other internal states
136    int m_icount;
137   UINT64 m_elapsed_cycles;
138
139   // memory access
140   inline UINT8 program_read8(UINT32 addr);
141   inline UINT16 program_read16(UINT32 addr);
142   inline void program_write8(UINT32 addr, UINT8 data);
143   inline void program_write16(UINT32 addr, UINT16 data);
144   inline UINT8 io_read8(UINT16 addr);
145   inline void io_write8(UINT16 addr, UINT8 data);
146   inline UINT16 opcode_read();
147   inline void push(UINT8 val);
148   inline UINT8 pop();
149   inline bool is_long_opcode(UINT16 op);
150
151   // utility
152   void unimplemented_opcode(UINT32 op);
153
154   // interrupts
155   void set_irq_line(UINT16 vector, int state);
156   void update_interrupt_internal(int source);
157
158   // timers
159   void timer_tick(int cycles);
160
161   // timer 0
162   void timer0_tick();
163
164   // timer 1
165   void timer1_tick();
166   void change_timsk1(UINT8 data);
167   void update_timer1_waveform_gen_mode();
168   void changed_tccr1a(UINT8 data);
169   void update_timer1_input_noise_canceler();
170   void update_timer1_input_edge_select();
171   void update_timer1_clock_source();
172   void changed_tccr1b(UINT8 data);
173   void update_ocr1(UINT16 newval, UINT8 reg);
174
175   // timer 2
176   void timer2_tick();
177   void update_timer2_waveform_gen_mode();
178   void changed_tccr2a(UINT8 data);
179   void update_timer2_clock_source();
180   void timer2_force_output_compare(int reg);
181   void changed_tccr2b(UINT8 data);
182   void update_ocr2(UINT8 newval, UINT8 reg);
183
184   // register Handling
185   bool io_reg_write(UINT16 offset, UINT8 data);
186   bool io_reg_read(UINT16 offset, UINT8 *data);
187
188   // address spaces
189    address_space *m_program;
190    address_space *m_io;
47191};
48192
193// device type definition
194extern const device_type ATMEGA88;
195extern const device_type ATMEGA644;
196
197// ======================> atmega88_device
198
199class atmega88_device : public avr8_device
200{
201public:
202   // construction/destruction
203   atmega88_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
204    : avr8_device(mconfig, tag, owner, clock, ATMEGA88, 0x0fff) { }
205};
206
207// ======================> atmega644_device
208
209class atmega644_device : public avr8_device
210{
211public:
212   // construction/destruction
213   atmega644_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
214    : avr8_device(mconfig, tag, owner, clock, ATMEGA644, 0xffff) { }
215};
216
217/***************************************************************************
218    REGISTER ENUMERATION
219***************************************************************************/
220
49221enum
50222{
51223    AVR8_SREG = 1,
r18789r18790
299471#define AVR8_SPCR_CPHA_MASK      0x04
300472#define AVR8_SPCR_SPR_MASK      0x03
301473
302DECLARE_LEGACY_CPU_DEVICE(ATMEGA88, atmega88);
303DECLARE_LEGACY_CPU_DEVICE(ATMEGA644, atmega644);
304
305void avr8_update_interrupt(device_t *device, int source);
306UINT64 avr8_get_elapsed_cycles(device_t *device);
307
308474CPU_DISASSEMBLE( avr8 );
309475
310476#endif /* __AVR8_H__ */
trunk/src/mess/drivers/craft.c
r18789r18790
9292
9393    UINT8 m_pixels[PIXELS_PER_FRAME];
9494
95    required_device<cpu_device> m_maincpu;
95    required_device<avr8_device> m_maincpu;
9696
9797   DECLARE_READ8_MEMBER(avr8_read);
9898   DECLARE_WRITE8_MEMBER(avr8_write);
r18789r18790
113113    switch( offset )
114114    {
115115      case AVR8_REGIDX_EEDR:
116      case AVR8_REGIDX_PORTC:
117      case AVR8_REGIDX_PORTD:
118116         return m_regs[offset];
119117
120118        default:
r18789r18790
166164{
167165    craft_state *state = machine.driver_data<craft_state>();
168166
169   UINT64 cycles = avr8_get_elapsed_cycles(state->m_maincpu);
167   UINT64 cycles = state->m_maincpu->get_elapsed_cycles();
170168   UINT32 frame_cycles = (UINT32)(cycles - state->m_frame_start_cycle);
171169
172170   if (state->m_last_cycles < frame_cycles)
r18789r18790
199197   state->m_last_cycles = frame_cycles;
200198}
201199
202static void avr8_change_port(running_machine &machine, int reg, UINT8 data)
200static void portb_write(avr8_device &device, UINT8 pins, UINT8 changed)
203201{
204    craft_state *state = machine.driver_data<craft_state>();
205
206   UINT8 oldport = avr8_get_ddr(machine, reg);
207   UINT8 newport = data;
208   UINT8 changed = newport ^ oldport;
209
210   // TODO: When AVR8 is converted to emu/machine, this should be factored out to 8 single-bit callbacks per port
211   if(changed)
202    craft_state *state = device.machine().driver_data<craft_state>();
203   if(pins & changed & 0x02)
212204   {
213      // TODO
214      //verboselog(machine, 0, "avr8_change_port: PORT%c lines %02x changed\n", avr8_reg_name[reg], changed);
205      state->m_frame_start_cycle = device.get_elapsed_cycles();
215206   }
207}
216208
217   switch(reg)
218   {
219      case AVR8_REG_A:
220         // Unhandled
221         break;
209static void portc_write(avr8_device &device, UINT8 pins, UINT8 changed)
210{
211    craft_state *state = device.machine().driver_data<craft_state>();
212   avr8_video_update(device.machine());
213   AVR8_PORTC = pins;
214}
222215
223      case AVR8_REG_B:
224         AVR8_PORTB = data;
225         if(newport & changed & 0x02)
226         {
227            state->m_frame_start_cycle = avr8_get_elapsed_cycles(state->m_maincpu);
228         }
229         break;
230
231      case AVR8_REG_C:
232         avr8_video_update(machine);
233         AVR8_PORTC = data;
234         break;
235
236      case AVR8_REG_D:
237      {
238         UINT8 audio_sample = (data & 0x02) | ((data & 0xf4) >> 2);
239         state->dac->write_unsigned8(audio_sample << 2);
240         AVR8_PORTD = data;
241         break;
242      }
243   }
216static void portd_write(avr8_device &device, UINT8 pins, UINT8 changed)
217{
218    craft_state *state = device.machine().driver_data<craft_state>();
219   UINT8 audio_sample = (pins & 0x02) | ((pins & 0xf4) >> 2);
220   state->dac->write_unsigned8(audio_sample << 1);
244221}
245222
246223/****************/
r18789r18790
304281   if(changed & AVR8_SPCR_SPIE_MASK)
305282   {
306283      // Check for SPI interrupt condition
307      avr8_update_interrupt(state->m_maincpu, AVR8_INTIDX_SPI);
284      state->m_maincpu->update_interrupt(AVR8_INTIDX_SPI);
308285   }
309286
310287   if(low_to_high & AVR8_SPCR_SPE_MASK)
r18789r18790
369346         break;
370347
371348      case AVR8_REGIDX_SPDR:
349      {
372350         avr8_video_update(machine());
373351         m_regs[offset] = data;
374352         m_spi_pending = true;
375         m_spi_start_cycle = avr8_get_elapsed_cycles(m_maincpu) - m_frame_start_cycle;
353         m_spi_start_cycle = m_maincpu->get_elapsed_cycles() - m_frame_start_cycle;
376354         break;
355      }
377356
378357      case AVR8_REGIDX_EECR:
379358         if (data & AVR8_EECR_EERE)
r18789r18790
389368         m_regs[offset] = data;
390369         break;
391370
392        case AVR8_REGIDX_PORTD:
393            avr8_change_port(machine(), AVR8_REG_D, data);
394            break;
395
396371      case AVR8_REGIDX_DDRD:
397372         avr8_change_ddr(machine(), AVR8_REG_D, data);
398373         break;
399374
400      case AVR8_REGIDX_PORTC:
401         avr8_change_port(machine(), AVR8_REG_C, data);
402         break;
403
404375      case AVR8_REGIDX_DDRC:
405376         avr8_change_ddr(machine(), AVR8_REG_C, data);
406377         break;
407378
408      case AVR8_REGIDX_PORTB:
409         avr8_change_port(machine(), AVR8_REG_B, data);
410         break;
411
412379      case AVR8_REGIDX_DDRB:
413380         avr8_change_ddr(machine(), AVR8_REG_B, data);
414381         break;
r18789r18790
487454    state->m_spi_start_cycle = 0;
488455}
489456
457const avr8_config atmega88_config =
458{
459   &portb_write,   // Video timing signals
460   &portc_write,   // Video color signals
461   &portd_write   // Audio DAC
462};
463
490464static MACHINE_CONFIG_START( craft, craft_state )
491465
492466    /* basic machine hardware */
493467    MCFG_CPU_ADD("maincpu", ATMEGA88, MASTER_CLOCK)
468    MCFG_CPU_AVR8_CONFIG(atmega88_config)
494469    MCFG_CPU_PROGRAM_MAP(craft_prg_map)
495470    MCFG_CPU_IO_MAP(craft_io_map)
496471
497
498472    /* video hardware */
499473    MCFG_SCREEN_ADD("screen", RASTER)
500474    MCFG_SCREEN_REFRESH_RATE(59.99)
trunk/src/mess/drivers/uzebox.c
r18789r18790
6060{
6161}
6262
63const avr8_config atmega644_config =
64{
65   NULL,
66   NULL,
67   NULL
68};
69
6370static MACHINE_CONFIG_START( uzebox, uzebox_state )
6471
6572   /* basic machine hardware */
6673   MCFG_CPU_ADD("maincpu", ATMEGA644, MASTER_CLOCK)
74    MCFG_CPU_AVR8_CONFIG(atmega644_config)
6775   MCFG_CPU_PROGRAM_MAP(uzebox_prg_map)
6876   MCFG_CPU_IO_MAP(uzebox_io_map)
6977
70
7178   /* video hardware */
7279   MCFG_SCREEN_ADD("screen", RASTER)
7380   MCFG_SCREEN_REFRESH_RATE(60.08)

Previous 199869 Revisions Next


© 1997-2024 The MAME Team