summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/patinhofeio/patinho_feio.cpp
blob: 9377ad259f8d8123e968823539a6ed4d6b641a44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// license:GPL-2.0+
// copyright-holders:Felipe Sanches
/*
    CPU emulation for Patinho Feio, the first computer designed and manufactured in Brazil
*/

#include "emu.h"
#include "debugger.h"
#include "patinho_feio.h"

#define PC       m_pc //The program counter is called "contador de instrucoes" in portuguese
#define ACC      m_acc
#define RC       read_panel_keys_register()
#define FLAGS    m_flags

#define V 0x01 // V = "Vai um" (Carry)
#define T 0x02 // T = "Transbordo" (Overflow)

#define READ_BYTE_PATINHO(A) (m_program->read_byte(A))
#define WRITE_BYTE_PATINHO(A,V) (m_program->write_byte(A,V))

#define READ_WORD_PATINHO(A) (READ_BYTE_PATINHO(A+1)*256 + READ_BYTE_PATINHO(A))

#define READ_INDEX_REG() READ_BYTE_PATINHO(0x000)
#define WRITE_INDEX_REG(V) { WRITE_BYTE_PATINHO(0x000, V); m_idx = V; }

#define ADDRESS_MASK_4K    0xFFF
#define INCREMENT_PC_4K    (PC = (PC+1) & ADDRESS_MASK_4K)

unsigned int patinho_feio_cpu_device::compute_effective_address(unsigned int addr){
    unsigned int retval = addr;
    if (m_indirect_addressing){
        retval = READ_WORD_PATINHO(addr);
        if (retval & 0x1000)
            return compute_effective_address(retval & 0xFFF);
    }

    return retval;
}

const device_type PATINHO_FEIO  = &device_creator<patinho_feio_cpu_device>;


//Internal 4kbytes of RAM
static ADDRESS_MAP_START(prog_8bit, AS_PROGRAM, 8, patinho_feio_cpu_device)
    AM_RANGE(0x0000, 0x0fff) AM_RAM AM_SHARE("internalram")
ADDRESS_MAP_END

patinho_feio_cpu_device::patinho_feio_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
    : cpu_device(mconfig, PATINHO_FEIO, "PATINHO FEIO", tag, owner, clock, "patinho_feio_cpu", __FILE__),
        m_program_config("program", ENDIANNESS_LITTLE, 8, 12, 0, ADDRESS_MAP_NAME(prog_8bit)),
        m_icount(0),
        m_rc_read_cb(*this)
{
}

UINT16 patinho_feio_cpu_device::read_panel_keys_register(){
    if (!m_rc_read_cb.isnull())
        m_rc = m_rc_read_cb(0);
    else
        m_rc = 0;

    return m_rc;
}

void patinho_feio_cpu_device::device_start()
{
    m_program = &space(AS_PROGRAM);

    save_item(NAME(m_pc));
    save_item(NAME(m_acc));
    save_item(NAME(m_rc));
    save_item(NAME(m_idx));
    save_item(NAME(m_flags));

    // Register state for debugger
    state_add( PATINHO_FEIO_CI,         "CI",       m_pc         ).mask(0xFFF);
    state_add( PATINHO_FEIO_RC,         "RC",       m_rc         ).mask(0xFFF);
    state_add( PATINHO_FEIO_ACC,        "ACC",      m_acc        ).mask(0xFF);
    state_add( PATINHO_FEIO_IDX,        "IDX",      m_idx        ).mask(0xFF);
    state_add(STATE_GENPC, "GENPC", m_pc).formatstr("0%06O").noshow();
    state_add(STATE_GENFLAGS,  "GENFLAGS",  m_flags).noshow().formatstr("%8s");

    if (m_rc_read_cb.isnull()){
        fatalerror("Panel keys register not found!");
    } else {
        m_rc_read_cb.resolve();
    }

    m_icountptr = &m_icount;
}

void patinho_feio_cpu_device::device_reset()
{
    m_pc = 0x006;
    m_acc = 0;
    m_rc = 0;
    m_idx = READ_INDEX_REG();
    m_flags = 0;
    m_run = true;
    m_scheduled_IND_bit_reset = false;
    m_indirect_addressing = false;
}

/* execute instructions on this CPU until icount expires */
void patinho_feio_cpu_device::execute_run()
{
    do
    {
        if ((! m_run)){
            m_icount = 0;   /* if processor is stopped, just burn cycles */
        } else {
            m_idx = READ_INDEX_REG();
            read_panel_keys_register();
            
            execute_instruction();
            m_icount --;
        }
    }
    while (m_icount > 0);
}

/* execute one instruction */
void patinho_feio_cpu_device::execute_instruction()
{
    debugger_instruction_hook(this, PC);
    offs_t addr;
    bool skip;
    unsigned int tmp;
    unsigned char value, channel, function;
    unsigned char opcode = READ_BYTE_PATINHO(PC);
    INCREMENT_PC_4K;

    if (m_scheduled_IND_bit_reset)
        m_indirect_addressing = false;

    if (m_indirect_addressing)
        m_scheduled_IND_bit_reset = true;

    switch (opcode){
        case 0xD2:
            //XOR: Computes the bitwise XOR of an immediate into the accumulator
            ACC ^= READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            //TODO: update T and V flags
            return;
        case 0xD4:
            //NAND: Computes the bitwise XOR of an immediate into the accumulator
            ACC = ~(ACC & READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            //TODO: update T and V flags
            return;
        case 0xD8:
            //SOMI="Soma Imediato":
            //     Add an immediate into the accumulator
            ACC += READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            //TODO: update T and V flags
            return;
        case 0xDA:
            //CARI="Carrega Imediato":
            //     Load an immediate into the accumulator
            ACC = READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            return;
        case 0x80:
            //LIMPO:
            //    Clear accumulator and flags
            ACC = 0;
            FLAGS = 0;
            return;
        case 0x81:
            //UM="One":
            //    Load 1 into accumulator
            //    and clear the flags
            ACC = 1;
            FLAGS = 0;
            return;
        case 0x82:
            //CMP1:
            // Compute One's complement of the accumulator
            //    and clear the flags
            ACC = ~ACC;
            FLAGS = 0;
            return;
        case 0x83:
            //CMP2:
            // Compute Two's complement of the accumulator
            //    and updates flags according to the result of the operation
            ACC = ~ACC + 1;
            FLAGS = 0; //TODO: fix-me (I'm not sure yet how to compute the flags here)
            return;
        case 0x84:
            //LIM="Limpa":
            // Clear flags
            FLAGS = 0;
            return;
        case 0x85:
            //INC:
            // Increment accumulator
            ACC++;
            FLAGS = 0; //TODO: fix-me (I'm not sure yet how to compute the flags here)
            return;
        case 0x86:
            //UNEG="Um Negativo":
            // Load -1 into accumulator and clear flags
            ACC = -1;
            FLAGS = 0;
            return;
        case 0x87:
            //LIMP1:
            //    Clear accumulator, reset T and set V
            ACC = 0;
            FLAGS = V;
            return;
        case 0x88:
            //PNL 0:
            ACC = (RC & 0xFF);
            FLAGS = 0;
            return;
        case 0x89:
            //PNL 1:
            ACC = (RC & 0xFF) + 1;
            //TODO: FLAGS = ?;
            return;
        case 0x8A:
            //PNL 2:
            ACC = (RC & 0xFF) - ACC - 1;
            //TODO: FLAGS = ?;
            return;
        case 0x8B:
            //PNL 3:
            ACC = (RC & 0xFF) - ACC;
            //TODO: FLAGS = ?;
            return;
        case 0x8C:
            //PNL 4:
            ACC = (RC & 0xFF) + ACC;
            //TODO: FLAGS = ?;
            return;
        case 0x8D:
            //PNL 5:
            ACC = (RC & 0xFF) + ACC + 1;
            //TODO: FLAGS = ?;
            return;
        case 0x8E:
            //PNL 6:
            ACC = (RC & 0xFF) - 1;
            //TODO: FLAGS = ?;
            return;
        case 0x8F:
            //PNL 7:
            ACC = (RC & 0xFF);
            FLAGS = V;
            return;
        case 0x9A:
            //INIB="Inibe"
            //     disables interrupts
            m_interrupts_enabled = false;
            return;
        case 0x9B:
            //PERM="Permite"
            //     enables interrupts
            m_interrupts_enabled = true;
            return;
        case 0x9C:
            //ESP="Espera":
            //    Holds execution and waits for an interrupt to occur.
            m_run = false;
            m_wait_for_interrupt = true;
            return;
        case 0x9D:
            //PARE="Pare":
            //    Holds execution. This can only be recovered by
            //    manually triggering execution again by
            //    pressing the "Partida" (start) button in the panel
            m_run = false;
            m_wait_for_interrupt = false;
            return;
        case 0x9E:
            //TRI="Troca com Indexador":
            //     Exchange the value of the accumulator with the index register
            value = ACC;
            ACC = READ_INDEX_REG();
            WRITE_INDEX_REG(value);
            return;
        case 0x9F:
            //IND="Enderecamento indireto":
            //     Sets memory addressing for the next instruction to be indirect.
            m_indirect_addressing = true;
            m_scheduled_IND_bit_reset = false; //the next instruction execution will schedule it.
            return;
        case 0xD1:
            //Bit-Shift/Bit-Rotate instructions
            value = READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            for (int i=0; i<4; i++){
                if (value & (1<<i)){
                    /* The number of shifts or rotations is determined by the
                       ammount of 1 bits in the lower 4 bits of 'value' */
                    switch(value & 0xF0)
                    {
                        case 0x00:
                            //DD="Deslocamento para a Direita"
                            //    Shift right
                            FLAGS &= ~V;
                            if (ACC & 1)
                                FLAGS |= V;

                            ACC >>= 1;
                            break;
                        case 0x20:
                            //GD="Giro para a Direita"
                            //    Rotate right
                            FLAGS &= ~V;
                            if (ACC & 1)
                                FLAGS |= V;

                            ACC = ((ACC & 1) << 7) | (ACC >> 1);
                            break;
                        case 0x10: //DDV="Deslocamento para a Direita com Vai-um"
                                //     Shift right with Carry
                        case 0x30: //GDV="Giro para a Direita com Vai-um"
                                //     Rotate right with Carry

                            //both instructions are equivalent
                            if (FLAGS & V)
                                tmp = 0x100 | ACC;
                            else
                                tmp = ACC;

                            FLAGS &= ~V;
                            if (ACC & 1)
                                FLAGS |= V;

                            ACC = tmp >> 1;
                            break;
                        case 0x40: //DE="Deslocamento para a Esquerda"
                                //    Shift left
                            FLAGS &= ~V;
                            if (ACC & (1<<7))
                                FLAGS |= V;

                            ACC <<= 1;
                            break;
                        case 0x60: //GE="Giro para a Esquerda"
                                //    Rotate left
                            FLAGS &= ~V;
                            if (ACC & (1<<7))
                                FLAGS |= V;

                            ACC = (ACC << 1) | ((ACC >> 7) & 1);
                            break;
                        case 0x50: //DEV="Deslocamento para a Esquerda com Vai-um"
                                //     Shift left with Carry
                        case 0x70: //GEV="Giro para a Esquerda com Vai-um"
                                //     Rotate left with Carry

                            //both instructions are equivalent
                            if (FLAGS & V)
                                tmp = (ACC << 1) | 1;
                            else
                                tmp = (ACC << 1);

                            FLAGS &= ~V;
                            if (tmp & (1<<8))
                                FLAGS |= V;

                            ACC = tmp & 0xFF;
                            break;
                        case 0x80: //DDS="Deslocamento para a Direita com duplicacao de Sinal"
                                //     Rotate right with signal duplication
                            FLAGS &= ~V;
                            if (ACC & 1)
                                FLAGS |= V;

                            ACC = (ACC & (1 << 7)) | ACC >> 1;
                            break;
                        default:
                            printf("Illegal instruction: %02X %02X\n", opcode, value);
                            return;
                    }
                }
            }
            return;
    }

    switch (opcode & 0xF0){
        case 0x00:
            //PLA = "Pula": Jump to address
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            PC = addr;
            return;
        case 0x10:
            //PLAX = "Pula indexado": Jump to indexed address
            tmp = (opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            m_idx = READ_INDEX_REG();
            PC = compute_effective_address(m_idx + tmp);
            return;
        case 0x20:
            //ARM = "Armazena": Store the value of the accumulator into a given memory position
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            WRITE_BYTE_PATINHO(addr, ACC);
            return;
        case 0x30:
            //ARMX = "Armazena indexado": Store the value of the accumulator into a given indexed memory position
            tmp = (opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            m_idx = READ_INDEX_REG();
            addr = compute_effective_address(m_idx + tmp);
            WRITE_BYTE_PATINHO(addr, ACC);
            return;
        case 0x40:
            //CAR = "Carrega": Load a value from a given memory position into the accumulator
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            ACC = READ_BYTE_PATINHO(addr);
            return;
        case 0x50:
            //CARX = "Carga indexada": Load a value from a given indexed memory position into the accumulator
            tmp = (opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            m_idx = READ_INDEX_REG();
            addr = compute_effective_address(m_idx + tmp);
            ACC = READ_BYTE_PATINHO(addr);
            return;
        case 0x60:
            //SOM = "Soma": Add a value from a given memory position into the accumulator
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            ACC += READ_BYTE_PATINHO(addr);
            //TODO: update V and T flags
            return;
        case 0x70:
            //SOMX = "Soma indexada": Add a value from a given indexed memory position into the accumulator
            tmp = (opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            m_idx = READ_INDEX_REG();
            addr = compute_effective_address(m_idx + tmp);
            ACC += READ_BYTE_PATINHO(addr);
            //TODO: update V and T flags
            return;
        case 0xA0:
            //PLAN = "Pula se ACC negativo": Jump to a given address if ACC is negative
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            if ((signed char) ACC < 0)
                PC = addr;
            return;
        case 0xB0:
            //PLAZ = "Pula se ACC for zero": Jump to a given address if ACC is zero
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            if (ACC == 0)
                PC = addr;
            return;
        case 0xC0:
            //Executes I/O functions
            //TODO: Implement-me!
            value = READ_BYTE_PATINHO(PC);
            INCREMENT_PC_4K;
            channel = opcode & 0x0F;
            function = value & 0x0F;
            switch(value & 0xF0){
                case 0x10:
                    printf("Unimplemented FNC /%X%X instruction\n", channel, function);
                    break;
                case 0x20:
                    //SAL="Salta"
                    //    Skips a couple bytes if a condition is met
                    skip = false;
                    switch(function)
                    {
                        case 1:
                            if (m_peripherals[channel].io_status == DEVICE_READY)
                                skip = true;
                            break;
                        case 2:
                            if (m_peripherals[channel].device_is_ok)
                                skip = true;
                            break;
                        case 4:
                            if (m_peripherals[channel].IRQ_request == true)
                                skip = true;
                            break;
                    }

                    if (skip){
                        INCREMENT_PC_4K;
                        INCREMENT_PC_4K;
                    }
                    break;
                case 0x40:
                    printf("Unimplemented ENTR /%X0 instruction\n", channel);
                    break;
                case 0x80:
                    printf("Unimplemented SAI /%X0 instruction (ACC = 0x%02X '%c')\n", channel, ACC, ACC);
                    break;
            }
            return;
        case 0xE0:
            //SUS = "Subtrai um ou Salta": Subtract one from the data in the given address
            //                             or, if the data is zero, then simply skip a couple bytes.
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            value = READ_BYTE_PATINHO(addr);
            if (value > 0){
                WRITE_BYTE_PATINHO(addr, value-1);
            } else {
                INCREMENT_PC_4K;
                INCREMENT_PC_4K;
            }
            return;
        case 0xF0:
            //PUG = "Pula e guarda": Jump and store.
            //      It stores the return address to addr and addr+1
            //      And then jumps to addr+2
            addr = compute_effective_address((opcode & 0x0F) << 8 | READ_BYTE_PATINHO(PC));
            INCREMENT_PC_4K;
            WRITE_BYTE_PATINHO(addr, (PC >> 8) & 0x0F);
            WRITE_BYTE_PATINHO(addr+1, PC & 0xFF);
            PC = addr+2;
            return;
    }
    printf("unimplemented opcode: 0x%02X\n", opcode);
}

offs_t patinho_feio_cpu_device::disasm_disassemble(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram, UINT32 options)
{
    extern CPU_DISASSEMBLE( patinho_feio );
    return CPU_DISASSEMBLE_NAME(patinho_feio)(this, buffer, pc, oprom, opram, options);
}