summaryrefslogtreecommitdiff
path: root/src/vcpu.c
blob: c3e1e5e0476802e05f2ddf9ad4ecde0fa2f18ccc (plain)
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
#include "vcpu.h"
#include "mem.h"
#include "screen.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Current VCPU state
VCPU_State State;

void VCPUInit(){
  State.PC=ADDR_ROM;
  State.S=0;
  srand(time(NULL));
}

void VCPUFetch(){
  unsigned char byte[2];
  MemRead(byte,2,State.PC); // Little indian to -1 no +1
  State.opcode=byte[0];
  State.opcode=State.opcode<<8;
  State.opcode=State.opcode | byte[1];
  State.PC+=2;
}

void VCPUDecode(){
  State.X=(State.opcode>>8) & 0xF;
  State.Y=(State.opcode>>4) & 0xF;
  State.N=State.opcode & 0x0F;
  State.NN=State.opcode & 0xFF;
  State.NNN=State.opcode & 0x0FFF;
}

void VCPUDoubleDabble(unsigned char x, unsigned char *u, unsigned char *t, unsigned char *h){
  unsigned int bcd=x;
  for(int i=0;i<8;i++){
    bcd=bcd<<1;
    unsigned char byte=bcd & 0xFF;
    unsigned char units=(bcd>>8) & 0xF;
    unsigned char tens=(bcd>>12) & 0xF;
    unsigned char hundreds=(bcd>>16) & 0xF;
    //printf("hundreds:%04b tens:%04b units:%04b byte:%08b\n",hundreds,tens,units,byte);
    if(i<7){
      if(units>4)
        units+=3;
      if(tens>4)
        tens+=3;
      if(hundreds>4)
        hundreds+=3;
    }
    bcd = (hundreds<<16) | (tens << 12) | (units << 8) | byte;
  }
  *u=bcd>>8 & 0xF;
  *t=bcd>>12 & 0xF;
  *h=bcd>>16 & 0xF;
}

void VCPUExecute(){
  VCPUDump();
  switch(State.opcode >> 12){
  case 0x0: // Clear screen or return from subroutine
    if(State.N == 0x0){ // Clear screen
      ScreenClear();
    }
    else if(State.N == 0xE) { // Return from subroutine
      State.PC=State.stack[State.S];
      State.S--;
    }
    break;
    
  case 0x1: // Jump
    State.PC=State.NNN;
    break;
    
  case 0x2: // Call
    State.S++;
    State.stack[State.S]=State.PC;
    State.PC=State.NNN;
    break;
      
  case 0x3: // SE: VX, byte
    if(State.V[State.X]==State.NN)
      State.PC+=2;
    break;
    
  case 0x4: // SNE: VX, byte
    if(State.V[State.X]!=State.NN)
      State.PC+=2;
    break;
    
  case 0x5: // SE: VX, VY
    if(State.N == 0){
      if(State.V[State.X]==State.V[State.Y])
        State.PC+=2;
    }
    break;
    
  case 0x6:
    State.V[State.X]=State.NN;
    break;
      
  case 0x7:
    State.V[State.X]=State.V[State.X] + State.NN;
    break;
    
  case 0x8: // Register operations
    switch(State.N){
    case 0x0: // VX = VY
      State.V[State.X]=State.V[State.Y];
      break;
      
    case 0x1: // VX = VX OR VY
      State.V[State.X]=State.V[State.X] | State.V[State.Y];
      break;
      
    case 0x2: // VX = VX AND VY
      State.V[State.X]=State.V[State.X] & State.V[State.Y];
      break;
      
    case 0x3: // VX = VX XOR VY
      State.V[State.X]=State.V[State.X] ^ State.V[State.Y];
      break;
      
    case 0x4: // VX = VX + VY
      if((State.V[State.X] + State.V[State.Y]) > 255)
        State.V[REG_FLAG]=1;
      else
        State.V[REG_FLAG]=0;
      State.V[State.X]=State.V[State.X] + State.V[State.Y];
      break;
      
    case 0x5: // VX = VX - VY
      if(State.V[State.X] > State.V[State.Y])
        State.V[REG_FLAG]=1;
      else
        State.V[REG_FLAG]=0;
      State.V[State.X]=State.V[State.X] - State.V[State.Y];
      break;
      
    case 0x6: // VX = VX SHR 1
      if(State.V[State.X] & 0x1 == 1)
        State.V[REG_FLAG]=1;
      else
        State.V[REG_FLAG]=0;
      State.V[State.X]=State.V[State.X] >> 1;
      break;
      
    case 0x7: // VX = VY - VX
      if(State.V[State.Y] > State.V[State.X])
        State.V[REG_FLAG]=1;
      else
        State.V[REG_FLAG]=0;
      State.V[State.X]=State.V[State.Y] - State.V[State.X];
      break;
      
    case 0xE: // VX = VX SHL 1
      if(State.V[State.X] >> 7 == 1)
        State.V[REG_FLAG]=1;
      else
        State.V[REG_FLAG]=0;
      State.V[State.X]=State.V[State.X] << 1;
      break;
      
    }    
    break;
    
  case 0x9: // SNE: VX, VY
    if(State.N==0){
      if(State.V[State.X]!=State.V[State.Y])
        State.PC+=2;
    }
    break;
    
  case 0xA:
    State.I=State.NNN;
    break;
    
  case 0xB:
    State.PC=State.V[0]+State.NNN;
    break;
    
  case 0xC:
    unsigned short n = rand() % 255 + 1;
    State.V[State.X]=n & State.NN;
    break;
      
  case 0xD: // Draw a sprite
    int X=State.V[State.X]&63;
    int Y=State.V[State.Y]&31;
    State.V[REG_FLAG]=0; // Set flag to 0
    int width, height;
    ScreenWH(&width,&height);
    for(char row=0;row<State.N;row++){
      // Stop if row out of screen
      if(Y+row>=height)
        break;
      unsigned char sprite;
      MemRead(&sprite,1,State.I+row); // Load sprite
      // Draw sprite
      for(int shift=0;shift<8;shift++){
        // Stop if column is out of screen
        if(X+shift >= width)
          break;
        if(ScreenPixelApply(X+shift,Y+row,(sprite>>(7-shift))&0x1))
          State.V[REG_FLAG]=1;
      }
    }
    break;
    
  case 0xE:
    // TODO
    break;
    
  case 0xF:
    switch(State.NN){
    case 0x07: // Get timer
      State.V[State.X]=State.DT;
      break;
      
    case 0x0A:
      // TODO
      break;
      
    case 0x15: // Set timer
      State.DT=State.V[State.X];
      break;
      
    case 0x18: // Set sound timer
      State.ST=State.V[State.X];
      break;
      
    case 0x1E: // I = I + VX
      State.I=State.I+State.V[State.X];
      break;
      
    case 0x29:
      // TODO
      break;
      
    case 0x33:
      unsigned char units, tens, hundreds;
      VCPUDoubleDabble(State.V[State.X],&units,&tens,&hundreds);
      MemCopy(&hundreds,1,State.I);
      MemCopy(&tens,1,State.I+1);
      MemCopy(&units,1,State.I+2);
      //      printf("hundreds:%d tens:%d units:%d byte:%d\n",hundreds,tens,units,State.V[State.X]);
      break;
      
    case 0x55:
      MemCopy(State.V,0xF,State.I);
      break;
      
    case 0x65:
      MemRead(State.V,0xF,State.I);
      break;
      
    }
    break;
  }
}

void VCPUDump(){
  printf("opcode: 0x%04x\n",State.opcode&0xFFFF);
  printf("X: 0x%01x\n",State.X);
  printf("Y: 0x%01x\n",State.Y);
  printf("N: 0x%01x\n",State.N);
  printf("NN: 0x%02x\n",State.NN);
  printf("NNN: 0x%03x\n",State.NNN);
}