-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicUtil.cs
495 lines (440 loc) · 15 KB
/
picUtil.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using picsim.Instructions;
using picsim.Instructions.BitOrientedInstructions;
using picsim.Instructions.ByteOrientedInstructions;
using picsim.Instructions.ControlOperations;
using picsim.Instructions.LiteralOperations;
namespace picsim
{
class PicUtil
{
public List<Instruction> program = new List<Instruction>();
private Pic pic = new Pic();
private string[] _opcodesByteOriented = new[]
{
"000111", //ADDWF
"000101", //ANDWF
"0000011", //CLRF
"0000010", //CLRW
"001001", //COMF
"000011", //DECF
"001011", //DECFSZ
"001010", //INCF
"001111", //INCFSZ
"000100", //IORWF
"0010000", //MOVF
"0000001", //MOVWF
"0011010", //RLF
"0011000", //RRF
"0000100", //SUBWF
"0011100", //SWAPF
"0001100" //XORWF
};
private string[] _opcodesBitOriented = new[]
{
"0100", //BCF
"0101", //BSF
"0110", //BTFSC
"0111" //BTFSS
};
private string[] _opcodesLiteral = new[]
{
"111110", //ADDLW
"111001", //ANDLW
"100000", //CALL
"101000", //GOTO
"111000", //IORLW
"110000", //MOVLW
"110100", //RETLW
"111100", //SUBLW
"111010" //XORLW
};
private string[] _opCodesControl = new[]
{
"00000001100100", //CLRWDT
"00000000001001", //RETFIE
"00000000001000", //RETURN
"00000001000011", //SLEEP
"000000000000000" //NOP
};
public Pic PicObject
{
get => pic;
set => pic = value;
}
public void DecodeInstructions(int instructionsCode)
{
var code = instructionsCode & 0b11000000000000;
var ident = code >> 12;
var code2 = instructionsCode & 0b00111100000000;
var ident2 = code2 >> 8;
var code3 = instructionsCode & 0b00000011110000;
var ident3 = code3 >> 4;
var ident4 = instructionsCode & 0b00000000001111;
switch (ident)
{
case 0:
{
if ((ident2 == 0 && ident3 == 0) && (ident4 == 8))
{
//RETURN
program.Add(new Return(instructionsCode, pic));
}
if ((ident2 == 0 && ident3 == 0) && (ident4 == 9))
{
//RETFIE
program.Add(new retfie(instructionsCode, pic));
}
if (ident2 == 0 && ident3 > 7)
{
//MOVWF
program.Add(new movwf(instructionsCode, pic));
}
if (ident2 == 7)
{
//ADDWF
program.Add(new addwf(instructionsCode, pic));
}
if (ident2 == 1 && ident3 > 7)
{
//CLRF
program.Add(new clrf(instructionsCode, pic));
}
if (ident2 == 1 && ident3 < 8)
{
//CLRW
program.Add(new clrw(instructionsCode, pic));
}
if (ident2 == 5)
{
//ANDWF
program.Add(new andwf(instructionsCode, pic));
}
if (ident2 == 9)
{
//COMPF
program.Add(new comf(instructionsCode, pic));
}
if (ident2 == 3)
{
//DECF
program.Add(new decf(instructionsCode, pic));
}
if (ident2 == 10)
{
//INCF
program.Add(new incf(instructionsCode, pic));
}
if (ident2 == 4)
{
//IORWF
program.Add(new iorwf(instructionsCode, pic));
}
if (ident2 == 8)
{
//MOVF
program.Add(new movf(instructionsCode, pic));
}
if (ident2 == 6)
{
//XORWF
program.Add(new xorwf(instructionsCode, pic));
}
if (ident2 == 14)
{
//SWAPF
program.Add(new swapf(instructionsCode, pic));
}
if (ident2 == 2)
{
//SUBWF
program.Add(new subwf(instructionsCode, pic));
}
if (ident2 == 11)
{
//DECFSZ
program.Add(new decfsz(instructionsCode, pic));
}
if (ident2 == 13)
{
// qInfo() << "RLF" << "\n"; //RLF
program.Add(new rlf(instructionsCode, pic));
}
if (ident2 == 12)
{
// qInfo() << "RRF" << "\n"; //RRF
program.Add(new rrf(instructionsCode, pic));
}
if (ident2 == 15)
{
//INCFSZ
program.Add(new incfsz(instructionsCode, pic));
}
if ((ident2 == 0) && (ident3 == 0) && (ident4 == 0))
{
//nop
program.Add(new nop(instructionsCode, pic));
}
if ((ident2 == 0) && (ident3 == 6) && (ident4 == 3))
{
//sleep
program.Add(new sleep(instructionsCode, pic));
}
}
break;
case 1:
{
if ((ident2 & 0b1100) == 0)
{
//BCF
program.Add(new bcf(instructionsCode, pic));
}
if ((ident2 & 0b1100) == 4)
{
//BSF
program.Add(new bsf(instructionsCode, pic));
}
if ((ident2 & 0b1100) == 8)
{
//"BTFSC"
program.Add(new btfsc(instructionsCode, pic));
}
if ((ident2 & 0b1100) == 12)
{
// "BTFSS"
program.Add(new btfss(instructionsCode, pic));
}
}
break;
case 2:
{
if (ident2 > 7)
{
// goto
program.Add(new Goto(instructionsCode, pic));
}
if (ident2 < 8)
{
//call
program.Add(new call(instructionsCode, pic));
}
}
break;
case 3:
{
if (ident2 < 4)
{
//MOVLW
program.Add(new movlw(instructionsCode, pic));
}
if ((ident2 == 4) || (ident2 == 5) || (ident2 == 6) || (ident2 == 7))
{
//RETLW
program.Add(new retlw(instructionsCode, pic));
}
if (ident2 == 8)
{
//IORLW
program.Add(new iorlw(instructionsCode, pic));
}
if (ident2 == 9)
{
//ANDLW
program.Add(new andlw(instructionsCode, pic));
}
if (ident2 == 10)
{
//XORLW
program.Add(new xorlw(instructionsCode, pic));
}
if ((ident2 == 12) || (ident2 == 13))
{
//SUBLW
program.Add(new sublw(instructionsCode, pic));
}
if ((ident2 == 14) || (ident2 == 15))
{
//ADDLW
program.Add(new addlw(instructionsCode, pic));
}
}
break;
}
}
private string DecodeOpcode(int inst)
{
var instString = Convert.ToString(inst, 2);
var found = false;
var result = "";
switch (instString)
{
case "0":
result = "00000000000000";
found = true;
break;
case "10000":
result = "0010000";
found = true;
break;
case "1001":
result = "0010010";
found = true;
break;
case "1000":
result = "00000000001000";
found = true;
break;
default:
break;
}
//Byteoriented Instructions
if (found == false)
{
foreach (var opcode in _opcodesByteOriented)
{
instString.Substring(0, 7);
if (instString.StartsWith(opcode))
{
found = true;
result = opcode;
break;
}
}
}
//Bitoriented Instructions
if (found == false)
{
foreach (var opcode in _opcodesBitOriented)
{
instString.Substring(0, 4);
if (instString.StartsWith(opcode))
{
found = true;
result = opcode;
break;
}
}
}
//Literal Instructions
if (found == false)
{
foreach (var opcode in _opcodesLiteral)
{
instString.Substring(0, 6);
if (instString.StartsWith(opcode))
{
found = true;
result = opcode;
break;
}
}
}
//Control Options
if (found == false)
{
foreach (var opcode in _opCodesControl)
{
if (instString.StartsWith(opcode))
{
found = true;
result = opcode;
break;
}
}
}
return result;
}
public void Execute()
{
pic.checkinterrupt();
RefreshRegisters();
Debug.WriteLine(program[pic.ProgCntr]);
program[pic.ProgCntr].Execute();
pic.ProgCntr += 1;
RefreshRegisters();
}
public void InitPic()
{
for (int i = 0; i <= 127; i++)
{
pic.RamBank0.Add(new ramCell());
pic.RamBank1.Add(new ramCell());
}
for (int i = 0; i <= 127; i++)
{
pic.RamBank0[i].Address = i;
}
for (int i = 0; i <= 127; i++)
{
pic.RamBank1[i].Address = i + 128;
}
pic.Wreg = 0;
//INDF
pic.RamBank0[0x00].Value = 0;
//TMR0
pic.RamBank0[0x01].Value = 0;
//PCL
pic.RamBank0[0x02].Value = 0;
//STATUS
pic.RamBank0[0x03].Value = 0b_0001_1000;
//FSR
pic.RamBank0[0x04].Value = 0;
//PORTA
pic.RamBank0[0x05].Value = 0;
//PORTB
pic.RamBank0[0x06].Value = 0;
//EEDATA
pic.RamBank0[0x08].Value = 0;
//EEADR
pic.RamBank0[0x09].Value = 0;
//PCLATH
pic.RamBank0[0x0A].Value = 0;
//INTCON
pic.RamBank0[0x0B].Value = 0;
//INDF
pic.RamBank1[0x00].Value = 0;
//OPTION_REG
pic.RamBank1[0x01].Value = 0b_1111_1111;
//PCL
pic.RamBank1[0x02].Value = 0;
//STATUS
pic.RamBank1[0x03].Value = 0b_0001_1000;
//FSR
pic.RamBank1[0x04].Value = 0;
//TRISA
pic.RamBank1[0x05].Value = 0b_0001_1111;
//TRISB
pic.RamBank1[0x06].Value = 0b_1111_1111;
//EECON1
pic.RamBank1[0x08].Value = 0;
//EECON2
pic.RamBank1[0x09].Value = 0;
//PCLATH
pic.RamBank1[0x0A].Value = 0;
//INTCON
pic.RamBank1[0x0B].Value = 0;
}
private void RefreshRegisters()
{
SetPclath();
pic.RamBank1[0x02].Value = pic.RamBank0[0x02].Value; //mirroring of PCL
pic.RamBank1[0x03].Value = pic.RamBank0[0x03].Value; //mirroring of STATUS
pic.RamBank1[0x04].Value = pic.RamBank0[0x04].Value; //mirroring of FSR
pic.RamBank1[0x0A].Value = pic.RamBank0[0x0A].Value; //mirroring of PCLATH
pic.RamBank1[0x0B].Value = pic.RamBank0[0x0B].Value; //mirroring of INTCON
pic.RamBank1[0x00].Value = pic.RamBank0[0x00].Value; //mirroring of INDF
pic.RamBank0[0x02].Value = pic.ProgCntr & 0b_1111_1111; //PCL
}
private void SetPclath()
{
var temp = pic.RamBank0[0x02].Value;
var temp1 = temp & 0b_1_1111_0000_0000;
var result = pic.RotateRight(Convert.ToUInt16(temp1), 17);
pic.RamBank0[0x0A].Value = Convert.ToInt32(result);
}
}
}