-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathavr8.cpp
More file actions
2559 lines (2216 loc) · 79.7 KB
/
Copy pathavr8.cpp
File metadata and controls
2559 lines (2216 loc) · 79.7 KB
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
(The MIT License)
Copyright (c) 2008-2016 by
David Etherton, Eric Anderton, Alec Bourque (Uze), Filipe Rinaldi,
Sandor Zsuga (Jubatian), Matt Pandina (Artcfox)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Revision Log
------------
7/8/2013 V1.16 Added emulation for Timer1 Overflow interrupt
More info at uzebox.org
*/
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include "avr8.h"
#ifndef NOGDB
#include "gdbserver.h"
#endif // NOGDB
#include "SDEmulator.h"
#include "Keyboard.h"
#define X ((XL)|(XH<<8))
#define DEC_X (XL-- || XH--)
#define INC_X (++XL || ++XH)
#define Y ((YL)|(YH<<8))
#define DEC_Y (YL-- || YH--)
#define INC_Y (++YL || ++YH)
#define Z ((ZL)|(ZH<<8))
#define DEC_Z (ZL-- || ZH--)
#define INC_Z (++ZL || ++ZH)
#define SP (SPL | (SPH<<8))
#define DEC_SP (SPL-- || SPH--)
#define INC_SP (++SPL || ++SPH)
#define SREG_I 7
#define SREG_T 6
#define SREG_H 5
#define SREG_S 4 // == N ^ V
#define SREG_V 3
#define SREG_N 2
#define SREG_Z 1
#define SREG_C 0
#define EEPM1 0x20
#define EEPM0 0x10
#define EERIE 0x08
#define EEMPE 0x04
#define EEPE 0x02
#define EERE 0x01
//Interrupts vector adresses
#define INT_RESET 0x00
#define WDT 0x10
#define TIMER1_COMPA 0x1A
#define TIMER1_COMPB 0x1C
#define TIMER1_OVF 0x1E
#define SPI_STC 0x26
#define REG_TCNT1L 0x84
//TIFR1 flags
#define TOV1 1
#define OCF1A 2
#define OCF1B 4
//TIMSK1 flags
#define TOIE1 1
#define OCIE1A 2
#define OCIE1B 4
#define ICIE1 32
//TCCR1B flags
#define CS10 1
#define WGM12 8
//Watchdog flags
#define WDE 8
#define WDIE 64
#define WDIF 128
#define DELAY16MS 457142 //in cpu cycles
#define HSYNC_HALF_PERIOD 910 //in cpu cycles
#define HSYNC_PERIOD 1820 //in cpu cycles
#define SD_ENABLED() SDpath
/*
#define D3 ((insn >> 4) & 7)
#define R3 (insn & 7)
#define D4 ((insn >> 4) & 15)
#define R4 (insn & 15)
#define R5 ((insn & 15) | ((insn >> 5) & 0x10))
#define D5 ((insn >> 4) & 31)
#define K8 (((insn >> 4) & 0xF0) | (insn & 0xF))
#define k7 ((s16)(insn<<6)>>9)
#define k12 ((s16)(insn<<4)>>4)
*/
#define BIT(x,b) (((x)>>(b))&1)
#define C BIT(SREG,SREG_C)
// Delayed output flags. If either is set, there is a delayed out waiting to
// be written on the end of update_hardware, so the next cycle it can have
// effect. The "dly_out" variable contains these flags, and the "dly_xxx"
// variables the values to be written out.
#define DLY_TCCR1B 0x0001U
#define DLY_TCNT1 0x0002U
// Masks for SREG bits, use to combine them
#define SREG_IM (1U << SREG_I)
#define SREG_TM (1U << SREG_T)
#define SREG_HM (1U << SREG_H)
#define SREG_SM (1U << SREG_S)
#define SREG_VM (1U << SREG_V)
#define SREG_NM (1U << SREG_N)
#define SREG_ZM (1U << SREG_Z)
#define SREG_CM (1U << SREG_C)
// Clears bits. Use this on the bits which should change processing
// the given instruction.
inline static void clr_bits(u8 &dest, unsigned int bits)
{
dest = dest & (~bits);
}
// Inverse set bit: Sets if value is zero. Mostly for Z flag
inline static void set_bit_inv(u8 &dest, unsigned int bit, unsigned int value)
{
// Assume at most 16 bits input on value, makes it 0 or 1, the latter
// if the input was nonzero. The "& 1U" part is usually thrown away by
// the compiler (32 bits). The "& 0xFFFFU" part might also be thrown
// away depending on the input.
value = ((value & 0xFFFFU) - 1U) >> 31;
dest = dest | ((value & 1U) << bit);
}
// Set bit using only the lowest bit of 'value': if 1, sets the bit.
inline static void set_bit_1(u8 &dest, unsigned int bit, unsigned int value)
{
// The "& 1" on 'value' might be thrown away for suitable input.
dest = dest | ((value & 1U) << bit);
}
// Store bit (either set or clear) using only the lowest bit of 'value'.
inline static void store_bit_1(u8 &dest, unsigned int bit, unsigned int value)
{
// The "& 1" on 'value' might be thrown away for suitable input
// If 'bit' is constant (inlining), it folds up well on optimizing.
dest = dest & (~(1U << bit));
dest = dest | ((0U - (value & 1U)) & (1U << bit));
}
// This computes both the half-carry (bit3) and full carry (bit7)
#define BORROWS (~Rd&Rr)|(Rr&R)|(R&~Rd)
#define CARRIES ((Rd&Rr)|(Rr&~R)|(~R&Rd))
#define UPDATE_HC_SUB \
CH = BORROWS; \
set_bit_1(SREG, SREG_H, (CH & 0x08U) >> 3); \
set_bit_1(SREG, SREG_C, (CH & 0x80U) >> 7);
#define UPDATE_HC_ADD \
CH = CARRIES; \
set_bit_1(SREG, SREG_H, (CH & 0x08U) >> 3); \
set_bit_1(SREG, SREG_C, (CH & 0x80U) >> 7);
#define UPDATE_H set_bit_1(SREG, SREG_H, (CARRIES & 0x8) >> 3)
#define UPDATE_Z set_bit_inv(SREG, SREG_Z, R)
#define UPDATE_V_ADD set_bit_1(SREG, SREG_V, (((Rd&Rr&~R)|(~Rd&~Rr&R)) & 0x80) >> 7)
#define UPDATE_V_SUB set_bit_1(SREG, SREG_V, (((Rd&~Rr&~R)|(~Rd&Rr&R)) & 0x80) >> 7)
#define UPDATE_N set_bit_1(SREG, SREG_N, (R & 0x80) >> 7)
#define UPDATE_S set_bit_1(SREG, SREG_S, BIT(SREG,SREG_N) ^ BIT(SREG,SREG_V))
#define UPDATE_SVN_SUB UPDATE_V_SUB; UPDATE_N; UPDATE_S
#define UPDATE_SVN_ADD UPDATE_V_ADD; UPDATE_N; UPDATE_S
// Simplified version for logical insns.
// sreg_clr on S, V, and N should be called before this.
// If 7th bit of R is set:
// Sets N, sets S, clears V.
// If 7th bit of R is clear:
// Clears N, clears S, clears V.
#define UPDATE_SVN_LOGICAL \
SREG |= ((0x7FU - (unsigned int)(R)) >> 8) & (SREG_SM | SREG_NM);
#define UPDATE_CZ_MUL(x) set_bit_1(SREG,SREG_C,(x & 0x8000) >> 15); set_bit_inv(SREG,SREG_Z,x)
// UPDATE_CLEAR_Z: Updates Z flag by clearing if result is nonzero. This
// should be used if the previous Z flag state is meant to be preserved (such
// as in CPC), so don't include Z in a clr_bits then.
#define UPDATE_CLEAR_Z (SREG &= ~(((0U - (unsigned int)(R)) >> 8) & SREG_ZM))
#define SET_C (SREG |= (1<<SREG_C))
#define ILLEGAL_OP fprintf(stderr,"invalid insn at address %x\n",currentPc); shutdown(1);
#if defined(_DEBUG)
#define DISASM 1
#define DIS(fmt,...) sprintf(insnBuf,fmt,##__VA_ARGS__); if (disasmOnly) break
#else
#define DISASM 0
#define DIS(fmt,...)
#endif
u32 hsync_more_col;
u32 hsync_less_col;
#if !defined(__EMSCRIPTEN__) && !defined(__LIBRETRO__)
FILE* avconv_video = NULL;
FILE* avconv_audio = NULL;
#define SUPPORTS_RECORDING
#endif // __EMSCRIPTEN__
void avr8::spi_calculateClock(){
// calculate the number of cycles before the write completes
u16 spiClockDivider;
switch(SPCR & 0x03){
case 0: spiClockDivider = 4; break;
case 1: spiClockDivider = 16; break;
case 2: spiClockDivider = 64; break;
case 3: spiClockDivider = 128; break;
}
if(SPSR & 0x01){
spiClockDivider = spiClockDivider >> 1; // double the speed
}
spiCycleWait = spiClockDivider*8;
SPI_DEBUG("SPI divider set to : %d (%d cycles per byte)\n",spiClockDivider,spiCycleWait);
}
inline void avr8::write_io(u8 addr,u8 value)
{
// Pixel output ideally should inline, it is performed about 2 - 3
// million times per second in a Uzebox game.
if (addr == ports::PORTC)
{
pixel_raw = value & DDRC;
}
else
{
write_io_x(addr, value);
}
}
// Should not be called directly, use write_io instead (pixel output!)
void avr8::write_io_x(u8 addr,u8 value)
{
u8 changed;
u8 went_low;
switch (addr)
{
case (ports::OCR2A):
if (enableSound && TCCR2B)
{
// raw pcm sample at 15.7khz
#ifndef __EMSCRIPTEN__
adrv->flush();
#endif // __EMSCRIPTEN__
adrv->push(value);
#ifdef SUPPORTS_RECORDING
//Send audio byte to ffmpeg
if(recordMovie && avconv_audio) {
fwrite(&value, 1, 1, avconv_audio);
// Keep audio in sync, since the sample rate we encode at is not a factor of the clock speed
const double needs_extra_sample = 4.0 * 1.0 / 15734.0 / (1.0 / 15734.0 - 1820.0 / 28636360.0);
static double accumulated_error = 0.0;
accumulated_error += (28636360 % 15734);
if (accumulated_error > needs_extra_sample) {
accumulated_error -= needs_extra_sample;
fwrite(&value, 1, 1, avconv_audio);
}
}
#endif // __EMSCRIPTEN__
}
break;
case (ports::PORTD):
// write value with respect to DDRD register
io[addr] = value & DDRD;
break;
case (ports::PORTB):
if(value&1){
elapsedCycles=cycleCounter-prevCyclesCounter;
if (scanline_count == -999 && elapsedCycles >= HSYNC_HALF_PERIOD -10 && elapsedCycles <= HSYNC_HALF_PERIOD + 10)
{
scanline_count = scanline_top;
}
else if (scanline_count != -999)
{
if (scanline_count >= 0){
vdrv->render_line(scanline_count, &scanline_buf[0], left_edge + left_edge_cycle, palette);
}
scanline_count ++;
left_edge_cycle = cycleCounter;
if (scanline_count == 224)
{
vdrv->update_frame();
#ifdef SUPPORTS_RECORDING
//Send video frame to ffmpeg
if (recordMovie && avconv_video) vdrv->record_frame(avconv_video);
#endif // __EMSCRIPTEN__
SDL_Event event;
#ifndef NOGDB
while (singleStep ? idrv->wait(&event) : idrv->poll(&event))
#else // NOGDB
while (idrv->poll(&event))
#endif // NOGDB
{
switch (event.type) {
case SDL_KEYDOWN:
handle_key_down(event);
break;
case SDL_KEYUP:
handle_key_up(event);
break;
case SDL_QUIT:
printf("User abort (closed window).\n");
shutdown(0);
break;
}
}
//capture or replay controlelr capture data
if(captureMode==CAPTURE_WRITE){
fputc((u8)(idrv->buttons[0]&0xff),captureFile);
fputc((u8)((idrv->buttons[0]>>8)&0xff),captureFile);
}else if(captureMode==CAPTURE_READ && captureSize>0){
idrv->buttons[0]=captureData[capturePtr]+(captureData[capturePtr+1]<<8);
capturePtr+=2;
captureSize-=2;
}else if(captureMode==CAPTURE_READ && captureSize==0){
printf("Playback reached end of capture file.\n");
shutdown(0);
}
#ifndef NOGDB
singleStep = nextSingleStep;
#endif // NOGDB
scanline_count = -999;
}
}
prevCyclesCounter=cycleCounter;
}
break;
case (ports::PORTA):
changed = value ^ io[addr];
went_low = changed & io[addr];
if (went_low == (1<<2)) // LATCH
{
for (int i=0; i<2; i++)
{
latched_buttons[i] = idrv->buttons[i];
// don't let UP+DOWN register at same time
if ((latched_buttons[i] & ((1<<PAD_LEFT)|(1<<PAD_RIGHT))) == 0)
latched_buttons[i] |= (1<<PAD_RIGHT);
// same for LEFT+RIGHT
if ((latched_buttons[i] & ((1<<PAD_UP)|(1<<PAD_DOWN))) == 0)
latched_buttons[i] |= (1<<PAD_DOWN);
}
}
else if (went_low == (1<<3)) // CLOCK
{
if (new_input_mode) PINA = u8((latched_buttons[0] & 1) | ((latched_buttons[1] & 1) << 1));
latched_buttons[0] >>= 1;
latched_buttons[1] >>= 1;
if ((latched_buttons[1] < 0xFFFFF) && !new_input_mode)
{
//New input routines detected, switching emulation method
new_input_mode = true;
}
}
if (!new_input_mode) PINA = u8((latched_buttons[0] & 1) | ((latched_buttons[1] & 1) << 1));
//Uzebox keyboard (always on P2 port)
switch(uzeKbState){
case KB_STOP:
//check uzekeyboard start condition: clock=low & latch=high simultaneously
if((value&0x0c)==0x04){
uzeKbState=KB_TX_START;
uzeKbEnabled=true; //enable keyboard capture for Uzebox Keyboard
}
break;
case KB_TX_START:
//check start condition pulse completed: clock=high & latch=low (normal state)
if((value&0x0c)==0x08){
uzeKbState=KB_TX_READY;
uzeKbClock=8;
}
break;
case KB_TX_READY:
if (went_low == (1<<3)) // CLOCK
{
if(uzeKbClock==8){
uzeKbDataOut=0;
//returns only keys (no commands response yet)
if(uzeKbScanCodeQueue.empty()){
uzeKbDataIn=0;
}else{
uzeKbDataIn=uzeKbScanCodeQueue.front();
uzeKbScanCodeQueue.pop();
}
}
//shift data out to keyboard
//latch pin is used as "Data Out"
uzeKbDataOut<<=1;
if(value&0x04){ //latch pin=1?
uzeKbDataOut|=1;
}
//shift data in from keyboard
if(uzeKbDataIn&0x80){
PINA|=(0x02); //set P2 data bit
}else{
PINA&=~(0x02); //clear P2 data bit
}
uzeKbDataIn<<=1;
uzeKbClock--;
if(uzeKbClock==0){
if(uzeKbDataOut==KB_SEND_END){
uzeKbState=KB_STOP;
}else{
uzeKbClock=8;
}
}
}
break;
}
io[addr] = value;
break;
case (ports::TCNT1H):
// p106 in 644 manual; 16-bit values are latched
T16_latch = value;
break;
case (ports::TCNT1L):
dly_TCNT1L = value;
dly_TCNT1H = T16_latch;
dly_out |= DLY_TCNT1;
break;
case (ports::SPDR):
if((SPCR & 0x40) && SD_ENABLED()){ // only if SPI is enabled and card is present
spiByte = value;
//TODO: flag collision if x-fer in progress
spiClock = spiCycleWait;
spiTransfer = 1;
SPSR ^= 0x80; // clear interrupt
//SPI_DEBUG("spiClock: %0.2X\n",spiClock);
}
// SPI_DEBUG("SPDR: %0.2X\n",value);
io[addr] = value;
break;
case (ports::SPCR):
SPI_DEBUG("SPCR: %02X\n",value);
io[addr] = value;
if(SD_ENABLED()) spi_calculateClock();
break;
case (ports::SPSR):
SPI_DEBUG("SPSR: %02X\n",value);
io[addr] = value;
if(SD_ENABLED()) spi_calculateClock();
break;
case (ports::EECR):
//printf("writing to port %s (%x) pc = %x\n",port_name(addr),value,pc-1);
//EEPROM can only be put into either read or write mode, and the master bit must be set
if(value & EERE){
if(io[addr] & EEPE){
io[addr] = value ^ EERE; // programming in progress, don't allow this to be set
}
else{
io[addr] = value;
}
}
else if(value & EEPE){
if( (io[addr] & EERE) || !(io[addr] & EEMPE)){ // need master program enabled first
io[addr] = value ^ EEPE; // read in progress, don't allow this to be set
}
else{
io[addr] = value;
}
}
if(value & EEMPE){
io[addr] = value;
// eeClock = 4; TODO: This was only set here, never used. Maybe a never completed EEPROM timing code.
}
else{
io[addr] = value;
}
break;
// Note: This was commented out in the original code. If needed,
// integrate in the switch.
//else if(addr == ports::EEARH || addr == ports::EEARL || addr == ports::EEDR){
// io[addr] = value;
case (ports::TIFR1):
//clear flags by writing logical one
io[addr] &= ~(value);
break;
case (ports::TCCR1B):
dly_TCCR1B = value;
dly_out |= DLY_TCCR1B;
break;
case (ports::OCR1AH):
case (ports::OCR1AL):
case (ports::OCR1BH):
case (ports::OCR1BL):
// TODO: These should also be latched by the Atmel docs, maybe
// implement it later.
io[addr] = value;
TCNT1 += timer1_base - timer1_next;
timer1_base = 0U;
timer1_next = 0U; // Force timer state recalculation (update_hardware)
break;
case (ports::res3A):
// emulator-only whisper support
printf("%c",value);
break;
case (ports::res39):
// emulator-only whisper support
printf("%02x",value);
break;
default:
io[addr] = value;
break;
}
}
u8 avr8::read_io(u8 addr)
{
// p106 in 644 manual; 16-bit values are latched
if (addr == ports::TCNT1L)
{
unsigned int curr_timer = TCNT1 + timer1_base - timer1_next;
T16_latch = (curr_timer >> 8) & 0xFFU;
return curr_timer & 0xFFU;
}
else if (addr == ports::TCNT1H)
{
return T16_latch;
}
else
{
return io[addr];
}
}
// Inline variation of update_hardware, to be used with frequent multi-cycle
// instructions.
inline void avr8::update_hardware_fast()
{
if (timer1_next == 0U)
{
update_hardware();
return;
}
cycleCounter ++;
timer1_next --;
scanline_buf[cycleCounter & 0x7FFU] = pixel_raw;
}
// Performs hardware updates which have to be calculated at cycle precision
void avr8::update_hardware()
{
cycleCounter ++;
// timer1_next stores the cycles remaining until the next event on the
// Timer1 16 bit timer. It can be cleared to zero whenever the timer's
// state is changed (port writes). Locking it to zero should have no
// effect, causing the timer to re-calculate its state proper on every
// update_hardware call. It should only improve performance.
if (timer1_next == 0U)
{
// Apply time elapsed between full timer processings
TCNT1 += timer1_base;
// Apply delayed timer interrupt flags
TIFR1 |= itd_TIFR1;
itd_TIFR1 = 0U;
// Process timer
if ((TCCR1B & 7U) != 0U) // If timer 1 is started
{
unsigned int OCR1A = OCR1AL | ((unsigned int)(OCR1AH) << 8);
unsigned int OCR1B = OCR1BL | ((unsigned int)(OCR1BH) << 8);
if(TCCR1B & WGM12) // Timer in CTC mode: count up to OCRnA then resets to zero
{
if (TCNT1 == 0xFFFFU)
{
itd_TIFR1 |= TOV1;
}
if (TCNT1 == OCR1B)
{
itd_TIFR1 |= OCF1B;
}
if (TCNT1 == OCR1A)
{
TCNT1 = 0U;
itd_TIFR1 |= OCF1A;
}
else
{
TCNT1 = (TCNT1 + 1U) & 0xFFFFU;
}
// Calculate next timer event
if (itd_TIFR1 == 0U)
{
timer1_next = 0xFFFFU - TCNT1;
if ( (TCNT1 <= OCR1B) &&
(timer1_next > (OCR1B - TCNT1)) )
{
timer1_next = (OCR1B - TCNT1);
}
if ( (TCNT1 <= OCR1A) &&
(timer1_next > (OCR1A - TCNT1)) )
{
timer1_next = (OCR1A - TCNT1);
}
}
}else{ //timer in normal mode: counts up to 0xffff then rolls over
if (TCNT1 == 0xFFFFU)
{
itd_TIFR1 |= TOV1;
}
TCNT1 = (TCNT1 + 1U) & 0xFFFFU;
// Calculate next timer event
if (itd_TIFR1 == 0U)
{
timer1_next = 0xFFFFU - TCNT1;
}
}
}
// Set timer base to be able to reproduce TCNT1 outside full timer
// processing
timer1_base = timer1_next;
}
else
{
timer1_next --;
}
// Draw pixel on scanline
scanline_buf[cycleCounter & 0x7FFU] = pixel_raw;
}
// Performs hardware updates which can be done at instruction precision
// Also process interrupt requests
inline void avr8::update_hardware_ins()
{
// Apply delayed outputs
//
// Notes: This can be here since as of now, it looks like all
// instructions writing IO registers perform that in their last
// cycle, so a delayed output may only be produced then, that is,
// after the instruction. If this doesn't hold up, this will have
// to be placed in the update_hardware call.
if (dly_out != 0U)
{
if ((dly_out & DLY_TCCR1B) != 0U)
{
TCCR1B = dly_TCCR1B;
TCNT1 += timer1_base - timer1_next;
timer1_base = 0U;
timer1_next = 0U; // Timer state changes
}
if ((dly_out & DLY_TCNT1) != 0U)
{
TCNT1 = (dly_TCNT1H << 8) | dly_TCNT1L;
timer1_base = 0U;
timer1_next = 0U; // Timer state changes
}
dly_out = 0U;
}
// Get cycle count to emulate
unsigned int cycles = cycleCounter - cycle_ctr_ins;
cycle_ctr_ins = cycleCounter;
// Notes:
//
// From this point if further cycles are required to be consumed,
// those should be consumed using update_hardware(). This won't
// increase this run's cycle count (cycles), but will show in the
// next run proper.
// Watchdog notes:
//
// This is a bare minimum implementation to make the Uzebox kernel's
// seed generator operational (used for seeding a PRNG).
if(WDTCSR & WDE){ //if watchdog enabled
watchdogTimer += cycles;
if(watchdogTimer>=DELAY16MS && (WDTCSR&WDIE)){
WDTCSR|=WDIF; //watchdog interrupt
//reset watchdog
//watchdog is based on a RC oscillator
//so add some random variation to simulate entropy
watchdogTimer=rand()%1024;
}
}
// clock the SPI hardware.
if((SPCR & 0x40) && SD_ENABLED()){ // only if SPI is enabled
//TODO: test for master/slave modes (assume master for now)
// TODO: factor in clock divider
if(spiTransfer){
if(spiClock <= cycles){
//SPI_DEBUG("SPI transfer complete\n");
update_spi();
spiClock = 0;
spiTransfer = 0;
SPSR |= 0x80; // set interrupt
}
else{
spiClock -= cycles;
}
}
/*
//HACK: instantaneous SPI access
if(spiTransfer && spiClock > 0){
update_spi();
SPSR |= 0x80; // set interrupt
spiTransfer = 0;
spiClock = 0;
}*/
// test for interrupt (enable and interrupt flag for SPI)
// TODO (Jubatian): Verify that the move is OK, if not, try to fix
// it there (where the other interrupts are)
}
//clock the EEPROM hardware
/*
1. Wait until EEPE becomes zero.
2. Wait until SELFPRGEN in SPMCSR becomes zero.
3. Write new EEPROM address to EEAR (optional).
4. Write new EEPROM data to EEDR (optional).
5. Write a logical one to the EEMPE bit while writing a zero to EEPE in EECR.
6. Within four clock cycles after setting EEMPE, write a logical one to EEPE.
The EEPROM can not be programmed during a CPU write to the Flash memory.
*/
// are we attempting to program?
// TODO (Jubatian):
//
// cycleCounter is incremented here by 4, but this has no effect on at
// least Timer 1 and the video output, maybe even more. Not like
// writing to EEPROM would be a common task when drawing the video
// frame, though.
if(EECR & (EEPE|EERE))
{
if(EECR & EEPE){
//printf("attempting write of EEPROM\n");
cycleCounter += 4; // writes take four cycles
int addr = (EEARH << 8) | EEARL;
if(addr < eepromSize) eeprom[addr] = EEDR;
EECR ^= (EEMPE | EEPE); // clear program bits
// interrupt?
//if((EECR & EERIE) && BIT(SREG,SREG_I)){
// SPSR ^= 0x80; // clear the interrupt
// trigger_interrupt(SPI_STC); // execute the vector
//}
}
// are we attempting to read?
else if(EECR & EERE){
// printf("attempting read of EEPROM\n");
cycleCounter += 4; // eeprom reads take 4 additonal cycles
int addr = (EEARH << 8) | EEARL;
if(addr < eepromSize) EEDR = eeprom[addr];
EECR ^= EERE; // clear read bit
// interrupt?
//if((EECR & EERIE) && BIT(SREG,SREG_I)){
// SPSR ^= 0x80; // clear the interrupt
// trigger_interrupt(SPI_STC); // execute the vector
//}
}
}
// Process interrupts in order of priority
if(SREG & (1<<SREG_I))
{
// Note (Jubatian):
// The SD card's SPI interrupt trigger was within the SPI
// handling part in update_hardware, however it belongs to
// interrupt triggers. Priority order might be broken (but
// essentially the emulator behaved according to this order
// prior to this move).
if((SPCR & 0x80) && (SPSR & 0x80))
{
// TODO: verify that SPI is dependent upon the global interrupt flag
SPSR ^= 0x80; // Clear the interrupt
trigger_interrupt(SPI_STC);
}
else if ((WDTCSR&(WDIF|WDIE))==(WDIF|WDIE))
{
WDTCSR&= ~WDIF; // Clear watchdog flag
trigger_interrupt(WDT);
}
else if((TIFR1 & (OCF1A|OCF1B|TOV1)) && (TIMSK1&(OCIE1A|OCIE1B|TOIE1)))
{
if ((TIFR1 & OCF1A) && (TIMSK1 & OCIE1A) )
{
TIFR1&= ~OCF1A; // Clear CTC match flag
trigger_interrupt(TIMER1_COMPA);
}
else if ((TIFR1 & OCF1B) && (TIMSK1 & OCIE1B))
{
TIFR1&= ~OCF1B; // Clear CTC match flag
trigger_interrupt(TIMER1_COMPB);
}
else if ((TIFR1 & TOV1) && (TIMSK1 & TOIE1))
{
TIFR1&= ~TOV1; // Clear TOV1 flag
trigger_interrupt(TIMER1_OVF);
}
}
}
}
instructionList_t instructionList[] = {
{ 1,"ADC r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0001110000000000, 0b0000000111110000, 0b0000001000001111},
{ 2,"ADD r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0000110000000000, 0b0000000111110000, 0b0000001000001111},
{ 3,"ADIW r%d, %d " , 1, 2, 24, 0, 3, 1, 0, 0, 1, 2, 0b1001011000000000, 0b0000000000110000, 0b0000000011001111},
{ 4,"AND r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0010000000000000, 0b0000000111110000, 0b0000001000001111},
{ 5,"ANDI r%d, %d " , 1, 1, 16, 0, 3, 1, 0, 0, 1, 1, 0b0111000000000000, 0b0000000011110000, 0b0000111100001111},
{ 6,"ASR r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000101, 0b0000000111110000, 0b0000000000000000},
{ 7,"BCLR %d " , 7, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010010001000, 0b0000000001110000, 0b0000000000000000},
{ 8,"BLD r%d, %d " , 1, 1, 0, 0, 6, 1, 0, 0, 1, 1, 0b1111100000000000, 0b0000000111110000, 0b0000000000000111},
{ 9,"BRBC %d, %d " , 7, 1, 0, 0, 3, 1, 0, 1, 1, 2, 0b1111010000000000, 0b0000000000000111, 0b0000001111111000},
{ 10,"BRBS %d, %d " , 7, 1, 0, 0, 3, 1, 0, 1, 1, 2, 0b1111000000000000, 0b0000000000000111, 0b0000001111111000},
{ 11,"BREAK " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010110011000, 0b0000000000000000, 0b0000000000000000},
{ 12,"BSET %d " , 7, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000001000, 0b0000000001110000, 0b0000000000000000},
{ 13,"BST r%d, %d " , 1, 1, 0, 0, 6, 1, 0, 0, 1, 1, 0b1111101000000000, 0b0000000111110000, 0b0000000000000111},
{ 14,"CALL %d (+ next word) " , 0, 1, 0, 0, 3, 1, 0, 0, 2, 4, 0b1001010000001110, 0b0000000000000000, 0b0000000111110001},
{ 15,"CBI io%d, %d " , 8, 1, 0, 0, 6, 1, 0, 0, 1, 2, 0b1001100000000000, 0b0000000011111000, 0b0000000000000111},
{ 16,"COM r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000000, 0b0000000111110000, 0b0000000000000000},
{ 17,"CP r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0001010000000000, 0b0000000111110000, 0b0000001000001111},
{ 18,"CPC r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0000010000000000, 0b0000000111110000, 0b0000001000001111},
{ 19,"CPI r%d, %d " , 1, 1, 16, 0, 3, 1, 0, 0, 1, 1, 0b0011000000000000, 0b0000000011110000, 0b0000111100001111},
{ 20,"CPSE r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 3, 0b0001000000000000, 0b0000000111110000, 0b0000001000001111},
{ 21,"DEC r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000001010, 0b0000000111110000, 0b0000000000000000},
{ 22,"EOR r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0010010000000000, 0b0000000111110000, 0b0000001000001111},
{ 23,"FMUL r%d, r%d " , 1, 1, 16, 0, 2, 1, 16, 0, 1, 2, 0b0000001100001000, 0b0000000001110000, 0b0000000000000111},
{ 24,"FMULS r%d, r%d " , 1, 1, 16, 0, 2, 1, 16, 0, 1, 2, 0b0000001110000000, 0b0000000001110000, 0b0000000000000111},
{ 25,"FMULSU r%d, r%d " , 1, 1, 16, 0, 2, 1, 16, 0, 1, 2, 0b0000001110001000, 0b0000000001110000, 0b0000000000000111},
{ 26,"ICALL " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001010100001001, 0b0000000000000000, 0b0000000000000000},
{ 27,"IJMP " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001010000001001, 0b0000000000000000, 0b0000000000000000},
{ 28,"IN r%d, io%d " , 1, 1, 0, 0, 8, 1, 0, 0, 1, 1, 0b1011000000000000, 0b0000000111110000, 0b0000011000001111},
{ 29,"INC r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000011, 0b0000000111110000, 0b0000000000000000},
{ 30,"JMP %d (+ next word) " , 0, 1, 0, 0, 3, 1, 0, 0, 2, 3, 0b1001010000001100, 0b0000000000000000, 0b0000000111110001},
{ 31,"LD r%d, -X " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000001110, 0b0000000111110000, 0b0000000000000000},
{ 32,"LD r%d, -Y " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000001010, 0b0000000111110000, 0b0000000000000000},
{ 33,"LD r%d, -Z " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000000010, 0b0000000111110000, 0b0000000000000000},
{ 34,"LD r%d, X " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000001100, 0b0000000111110000, 0b0000000000000000},
{ 35,"LD r%d, X+ " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000001101, 0b0000000111110000, 0b0000000000000000},
{ 36,"LD r%d, Y+ " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000001001, 0b0000000111110000, 0b0000000000000000},
{ 37,"LD r%d, Y+%d " , 1, 1, 0, 0, 5, 1, 0, 0, 1, 3, 0b1000000000001000, 0b0000000111110000, 0b0010110000000111},
{ 38,"LD r%d, Z+ " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000000001, 0b0000000111110000, 0b0000000000000000},
{ 39,"LD r%d, Z+%d " , 1, 1, 0, 0, 5, 1, 0, 0, 1, 3, 0b1000000000000000, 0b0000000111110000, 0b0010110000000111},
{ 40,"LDI r%d, %d " , 1, 1, 16, 0, 3, 1, 0, 0, 1, 1, 0b1110000000000000, 0b0000000011110000, 0b0000111100001111},
{ 41,"LDS r%d, %d (+next word) " , 1, 1, 0, 0, 0, 1, 0, 0, 2, 2, 0b1001000000000000, 0b0000000111110000, 0b0000000000000000},
{ 42,"LPM " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001010111001000, 0b0000000000000000, 0b0000000000000000},
{ 43,"LPM r%d, Z " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000000100, 0b0000000111110000, 0b0000000000000000},
{ 44,"LPM r%d, Z+ " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0b1001000000000101, 0b0000000111110000, 0b0000000000000000},
{ 45,"LSR r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000110, 0b0000000111110000, 0b0000000000000000},
{ 46,"MOV r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0010110000000000, 0b0000000111110000, 0b0000001000001111},
{ 47,"MOVW r%d, r%d " , 1, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0b0000000100000000, 0b0000000011110000, 0b0000000000001111},
{ 48,"MUL r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 2, 0b1001110000000000, 0b0000000111110000, 0b0000001000001111},
{ 49,"MULS r%d, r%d " , 1, 1, 16, 0, 2, 1, 16, 0, 1, 2, 0b0000001000000000, 0b0000000011110000, 0b0000000000001111},
{ 50,"MULSU r%d, r%d " , 1, 1, 16, 0, 2, 1, 16, 0, 1, 2, 0b0000001100000000, 0b0000000001110000, 0b0000000000000111},
{ 51,"NEG r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000001, 0b0000000111110000, 0b0000000000000000},
{ 52,"NOP " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000},
{ 53,"OR r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0010100000000000, 0b0000000111110000, 0b0000001000001111},
{ 54,"ORI r%d, %d " , 1, 1, 16, 0, 3, 1, 0, 0, 1, 1, 0b0110000000000000, 0b0000000011110000, 0b0000111100001111},
{ 55,"OUT io%d, r%d " , 8, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0b1011100000000000, 0b0000011000001111, 0b0000000111110000},
{ 56,"POP r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001000000001111, 0b0000000111110000, 0b0000000000000000},
{ 57,"PUSH r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000001111, 0b0000000111110000, 0b0000000000000000},
{ 58,"RCALL %d " , 0, 1, 0, 0, 3, 1, 0, 1, 1, 3, 0b1101000000000000, 0b0000000000000000, 0b0000111111111111},
{ 59,"RET " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 4, 0b1001010100001000, 0b0000000000000000, 0b0000000000000000},
{ 60,"RETI " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 4, 0b1001010100011000, 0b0000000000000000, 0b0000000000000000},
{ 61,"RJMP %d " , 0, 1, 0, 0, 3, 1, 0, 1, 1, 2, 0b1100000000000000, 0b0000000000000000, 0b0000111111111111},
{ 62,"ROR r%d " , 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010000000111, 0b0000000111110000, 0b0000000000000000},
{ 63,"SBC r%d, r%d " , 1, 1, 0, 0, 2, 1, 0, 0, 1, 1, 0b0000100000000000, 0b0000000111110000, 0b0000001000001111},
{ 64,"SBCI r%d, %d " , 1, 1, 16, 0, 3, 1, 0, 0, 1, 1, 0b0100000000000000, 0b0000000011110000, 0b0000111100001111},
{ 65,"SBI io%d, %d " , 8, 1, 0, 0, 6, 1, 0, 0, 1, 2, 0b1001101000000000, 0b0000000011111000, 0b0000000000000111},
{ 66,"SBIC io%d, %d " , 8, 1, 0, 0, 6, 1, 0, 0, 1, 3, 0b1001100100000000, 0b0000000011111000, 0b0000000000000111},
{ 67,"SBIS io%d, %d " , 8, 1, 0, 0, 6, 1, 0, 0, 1, 3, 0b1001101100000000, 0b0000000011111000, 0b0000000000000111},
{ 68,"SBIW r%d, %d " , 1, 2, 24, 0, 3, 1, 0, 0, 1, 2, 0b1001011100000000, 0b0000000000110000, 0b0000000011001111},
{ 69,"SBRC r%d, %d " , 2, 1, 0, 0, 6, 1, 0, 0, 1, 3, 0b1111110000000000, 0b0000000111110000, 0b0000000000000111},
{ 70,"SBRS r%d, %d " , 2, 1, 0, 0, 6, 1, 0, 0, 1, 3, 0b1111111000000000, 0b0000000111110000, 0b0000000000000111},
{ 71,"SLEEP " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010110001000, 0b0000000000000000, 0b0000000000000000},
{ 72,"SPM z+ " , 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0b1001010111101000, 0b0000000000000000, 0b0000000000000000},
{ 73,"ST -x, r%d " , 2, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000001110, 0b0000000111110000, 0b0000000000000000},
{ 74,"ST -y, r%d " , 2, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000001010, 0b0000000111110000, 0b0000000000000000},
{ 75,"ST -z, r%d " , 2, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000000010, 0b0000000111110000, 0b0000000000000000},
{ 76,"ST x, r%d " , 2, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000001100, 0b0000000111110000, 0b0000000000000000},
{ 77,"ST x+, r%d " , 2, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0b1001001000001101, 0b0000000111110000, 0b0000000000000000},