]> Shamusworld >> Repos - thunder/blob - src/v63701.h
Code cleanup, final fix for sprite lag problem.
[thunder] / src / v63701.h
1 //
2 // Virtual 63701 Header file
3 //
4 // by James Hammons
5 // (C) 2014 Underground Software
6 //
7 #ifndef __V63701_H__
8 #define __V63701_H__
9
10 #include <stdint.h>
11
12 // Useful defines
13
14 #define FLAG_H          0x20            // Half carry
15 #define FLAG_I          0x10            // IRQ
16 #define FLAG_N          0x08            // Negative
17 #define FLAG_Z          0x04            // Zero
18 #define FLAG_V          0x02            // oVerflow
19 #define FLAG_C          0x01            // Carry
20
21 #define V63701_LINE_RESET               0x0001  // v63701 RESET line
22 #define V63701_LINE_IRQ                 0x0002  // v63701 IRQ line
23 #define V63701_LINE_NMI                 0x0004  // v63701 NMI line
24 #define V63701_STATE_WAI                0x0008  // v63701 wait for IRQ line
25 #define V63701_TIMER_OVERFLOW   0x0010
26 #define V63701_OUTPUT_COMPARE   0x0020
27 #define V63701_INPUT_CAPTURE    0x0040
28 #define V63701_TRAP                             0x0080  // Illegal instruction executed flag
29 //#define V6809_START_DEBUG_LOG         0x8000  // Debug log go (temporary!)
30
31 // Useful structs
32
33 union Byte
34 {
35         struct
36         {
37                 uint8_t b0: 1; uint8_t b1: 1; uint8_t b2: 1; uint8_t b3: 1;
38                 uint8_t b4: 1; uint8_t b5: 1; uint8_t b6: 1; uint8_t b7: 1;
39         } bit;
40         uint8_t byte;
41 };
42
43 union Word
44 {
45         struct
46         {
47                 uint8_t lo: 8;
48                 uint8_t hi: 8;
49         } byte;
50         uint16_t word;
51 };
52
53 union Flags
54 {
55         struct
56         {
57                 uint8_t c: 1;
58                 uint8_t v: 1;
59                 uint8_t z: 1;
60                 uint8_t n: 1;
61                 uint8_t i: 1;
62                 uint8_t h: 1;
63         } flag;
64         uint8_t byte;
65 };
66
67 union TCSR
68 {
69         struct
70         {
71                 uint8_t olvl: 1;
72                 uint8_t iedg: 1;
73                 uint8_t etoi: 1;        // Enable Timer Over
74                 uint8_t eoci: 1;        // Enable Output Compare
75                 uint8_t eici: 1;        // Enable Input Capture
76                 uint8_t tof: 1;         // Timer Over
77                 uint8_t ocf: 1;         // Output Compare
78                 uint8_t icf: 1;         // Input Capture
79         } bit;
80         uint8_t byte;
81 };
82
83 union WordReg
84 {
85         struct
86         {
87                 uint8_t b: 8;
88                 uint8_t a: 8;
89         } acc;
90         uint16_t word;
91 };
92
93 struct V63701REGS
94 {
95         uint16_t pc;                                    // 63701 PC register
96         uint16_t x;                                             // 63701 X index register
97         uint16_t s;                                             // 63701 System stack pointer
98         uint8_t cc;                                             // 63701 Condition Code register
99 //      uint8_t a;                                              // 63701 A register
100 //      uint8_t b;                                              // 63701 B register
101         WordReg d;                                              // 63701 A & B registers (A is hi, B is lo)
102         TCSR tcsr;                                              // 63701 Timer control/Status register
103         Word counter;                                   // 63701 Counter register
104         Word outputCompare;                             // 63701 Output Compare register
105         uint8_t ddr1;
106         uint8_t ddr2;
107         uint8_t port1data;
108         uint8_t port2data;
109         uint8_t ramCtrl;
110         uint8_t cWriteLatch;                    // Counter write latch
111         uint8_t cReadLatch;                             // Counter read latch
112         bool tcsrWasRead;                               // TCSR was read flag
113         uint64_t clock;                                 // 63701 clock
114         uint8_t (* RdMem)(uint16_t);    // Address of uint8 read routine
115         void (* WrMem)(uint16_t, uint8_t);      // Address of uint8 write routine
116         uint32_t cpuFlags;                              // v63701 IRQ/RESET flags
117         uint32_t clockOverrun;                  // Amount of overflow between runs
118 };
119
120 // Function prototypes
121
122 void Execute63701(V63701REGS *, uint32_t);      // Function to execute 63701 instructions
123 uint64_t GetCurrentV63701Clock(void);           // Get the clock of the currently executing CPU
124
125 // It's the responsibility of the user to call these from their R/W handlers.
126 // These are usually memory mapped at $00-$1F.
127 uint8_t InternalRegisterRead(uint16_t);
128 void InternalRegisterWrite(uint16_t, uint8_t);
129
130 // These functions must be implemented by the user...
131 extern uint8_t V63701ReadPort1(void);
132 extern uint8_t V63701ReadPort2(void);
133 extern void V63701WritePort1(uint8_t);
134 extern void V63701WritePort2(uint8_t);
135
136 #endif  // __V63701_H__