-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tedmem.cpp
1404 lines (1279 loc) · 37 KB
/
Tedmem.cpp
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
/*<empty clipboard>
YAPE - Yet Another Plus/4 Emulator
The program emulates the Commodore 264 family of 8 bit microcomputers
This program is free software, you are welcome to distribute it,
and/or modify it under certain conditions. For more information,
read 'Copying'.
(c) 2000, 2001, 2004, 2005 Attila Grósz
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ctype.h>
#include "Tedmem.h"
#include "Sid.h"
#include "SIDSoundLib.h"
#include "Cpu.h"
#include "roms.h"
#include "Filter.h"
#define TEXTMODE 0x00000000
#define MULTICOLOR 0x00000010
#define GRAPHMODE 0x00000020
#define EXTCOLOR 0x00000040
#define REVERSE 0x00000080
#define ILLEGAL 0x0000000F
static unsigned int VertSubCount;
static int x,tmp;
static unsigned char *VideoBase;
ClockCycle TED::CycleCounter;
static bool ScreenOn, AttribFetch;
static bool SideBorderFlipFlop, CharacterWindow;
static unsigned int BadLine;
static unsigned int ClockingState;
static unsigned int CharacterCount = 0;
static bool VertSubActive;
static unsigned int CharacterPosition;
static unsigned int CharacterPositionReload;
static unsigned int TVScanLineCounter;
static bool HBlanking;
static bool VBlanking;
static bool aligned_write;
static unsigned char *aw_addr_ptr;
static unsigned char aw_value;
static unsigned int ff1d_latch;
TED *TED::instance_;
enum {
TSS = 1 << 1,
TDS = 1 << 2,
TRFSH = 1 << 3,
THALT1 = 1 << 4,
THALT2 = 1 << 5,
THALT3 = 1 << 6,
TDMA = 1 << 7
};
TED::TED() : filter(0), sidCard(0)
{
unsigned int i;
instance_ = this;
// clearing cartdridge ROMs
for (i=0;i<4;++i) {
memset(&(RomHi[i]),0,ROMSIZE);
memset(&(RomLo[i]),0,ROMSIZE);
memset(romlopath,0,sizeof(romlopath));
memset(romhighpath,0,sizeof(romhighpath));
};
// default ROM sets
strcpy(romlopath[0],"BASIC");
strcpy(romhighpath[0],"KERNAL");
// 64 kbytes of memory allocated
RAMMask=0xFFFF;
// actual ram bank pointer default setting
actram=Ram;
// setting screen memory pointer
scrptr=screen;
// pointer of the end of the screen memory
endptr=scrptr+456*312-8;
// setting the CPU to fast mode
fastmode=1;
// initial position of the electron beam (upper left corner)
irqline=VertSubCount=0;
beamy=0;
beamx=0;
hshift = 0;
scrblank= false;
charrombank=charrambank=cset=VideoBase=Ram;
scrattr=0;
timer1=timer2=timer3=0;
chrbuf = DMAbuf;
clrbuf = DMAbuf + 64;
tmpClrbuf = DMAbuf + 128;
memset(DMAbuf, 0, sizeof(DMAbuf));
// create an instance of the keyboard class
// keys = new KEYS;
// tap = new TAP;
// setting the TAP::mem pointer to this MEM class
// tap->mem=this;
// tcbmbus = NULL;
crsrblinkon = false;
VertSubActive = false;
CharacterPositionReload = CharacterPosition = 0;
SideBorderFlipFlop = false;
render_ok = false;
BadLine = 0;
CycleCounter = 0;
oscillatorInit();
memset(protectedPlayerMemory, 0xfe, sizeof(protectedPlayerMemory));
enableSidCard(1, 0);
}
void TED::Reset()
{
// clear RAM with powerup pattern
for (int i = 0; i < RAMSIZE; i++)
Ram[i] = (i >> 1) << 1 == i ? 0 : 0xFF;
// reset oscillators
oscillatorReset();
if (sidCard) sidCard->reset();
lastResetCycle = CycleCounter;
}
void TED::forcedReset()
{
ChangeMemBankSetup(false);
Reset();
}
void TED::texttoscreen(int x,int y, const char *scrtxt)
{
int i =0;
while (scrtxt[i]) {
chrtoscreen(x + i * 8, y, scrtxt[i]);
i++;
}
}
void TED::chrtoscreen(int x,int y, char scrchr)
{
int j, k;
unsigned char *charset = (unsigned char *) kernal+0x1000;
if (isalpha(scrchr)) {
scrchr=toupper(scrchr)-64;
charset+=(scrchr<<3);
for (j=0;j<8;j++)
for (k=0;k<8;k++)
(*(charset+j) & (0x80>>k)) ? screen[(y+j)*456+x+k]=0x00 : screen[(y+j)*456+x+k]=0x71;
return;
}
charset+=(scrchr<<3);
for (j=0;j<8;j++)
for (k=0;k<8;k++)
(*(charset+j) & (0x80>>k)) ? screen[(y+j)*456+x+k]=0x00 : screen[(y+j)*456+x+k]=0x71;
}
void TED::loadroms()
{
for (int i=0;i<4;i++) {
loadhiromfromfile(i,romhighpath[i]);
loadloromfromfile(i,romlopath[i]);
}
mem_8000_bfff = actromlo = &(RomLo[0][0]);
mem_fc00_fcff = mem_c000_ffff = actromhi = &(RomHi[0][0]);
}
void TED::loadloromfromfile(int nr, char fname[512])
{
FILE *img;
if (fname[0]!='\0') {
if (img = fopen(fname, "rb")) {
// load low ROM file
fread(&(RomLo[nr]),ROMSIZE,1,img);
fclose(img);
return;
}
switch (nr) {
case 0: memcpy(&(RomLo[0]),basic,ROMSIZE);
break;
case 1: if (!strncmp(fname,"3PLUS1LOW",9))
memcpy(&(RomLo[1]),plus4lo,ROMSIZE);
else
memset(&(RomLo[1]),0,ROMSIZE);
break;
default : memset(&(RomLo[nr]),0,ROMSIZE);
}
} else
memset(&(RomLo[nr]),0,ROMSIZE);
}
void TED::loadhiromfromfile(int nr, char fname[512])
{
FILE *img;
if (fname[0]!='\0') {
if (img = fopen(fname, "rb")) {
// load high ROM file
fread(&(RomHi[nr]),ROMSIZE,1,img);
fclose(img);
return;
}
switch (nr) {
case 0:
memcpy(&(RomHi[0]),kernal,ROMSIZE);
break;
case 1: if (!strncmp(fname,"3PLUS1HIGH",10))
memcpy(&(RomHi[1]),plus4hi,ROMSIZE);
else
memset(&(RomHi[1]),0,ROMSIZE);
break;
default : memset(&(RomHi[nr]),0,ROMSIZE);
}
} else
memset(&(RomHi[nr]),0,ROMSIZE);
}
void TED::injectCodeToRAM(unsigned int address, unsigned char *from, size_t len)
{
size_t bytes = (address + len > 0xffff) ? 0xffff - address : len;
memcpy(actram + (address & 0xffff), from, bytes);
}
void TED::copyToKbBuffer(char *bufferString, unsigned int bufferLength)
{
unsigned int bufferAddress = 0x0527;
if (bufferLength == -1U) bufferLength = (unsigned int) strlen(bufferString);
for (unsigned int i=0; i < bufferLength; i++)
Write( bufferAddress + i, bufferString[i]);
Write(0xEF, bufferLength);
}
ClockCycle TED::GetClockCount()
{
return CycleCounter;
}
void TED::ChangeMemBankSetup(bool romoff)
{
if (romoff) {
mem_8000_bfff = actram + (0x8000 & RAMMask);
mem_fc00_fcff = mem_c000_ffff = actram + (0xC000 & RAMMask);
} else {
mem_8000_bfff = actromlo;
mem_c000_ffff = actromhi;
mem_fc00_fcff = &(RomHi[0][0]);
}
}
unsigned char TED::Read(unsigned int addr)
{
switch ( addr & 0xF000 ) {
case 0x0000:
switch ( addr & 0xFFFF ) {
case 0:
return prddr;
case 1:
{
unsigned char retval =
(
//ReadBus()&
0xC0)
//|(tap->ReadCSTIn(CycleCounter)&0x10)
;
return (prp&prddr)|(retval&~prddr);
}
default:
return actram[addr&0xFFFF];
}
break;
case 0x1000:
case 0x2000:
case 0x3000:
return actram[addr&0xFFFF];
case 0x4000:
case 0x5000:
case 0x6000:
case 0x7000:
return actram[addr&RAMMask];
case 0x8000:
case 0x9000:
case 0xA000:
case 0xB000:
return mem_8000_bfff[addr&0x3FFF];
case 0xC000:
case 0xD000:
case 0xE000:
return mem_c000_ffff[addr&0x3FFF];
case 0xF000:
switch ( addr >> 8 ) {
case 0xFF:
switch (addr) {
case 0xFF00 : return timer1&0xFF;
case 0xFF01 : return timer1>>8;
case 0xFF02 : return timer2&0xFF;
case 0xFF03 : return timer2>>8;
case 0xFF04 : return timer3&0xFF;
case 0xFF05 : return timer3>>8;
case 0xFF06 : return Ram[0xFF06];
case 0xFF07 : return Ram[0xFF07];
case 0xFF08 :
#if 0
if (joyemu)
return keys->feedjoy(Ram[0xFF08])&(keys->feedkey(Ram[0xFD30]));
else
return keys->feedkey(Ram[0xFD30]);
#else
return 0xFF;
#endif
case 0xFF09 : return Ram[0xFF09]|(0x25);
case 0xFF0A : return Ram[0xFF0A]|(0xA0);
case 0xFF0B : return irqline & 0xFF;
case 0xFF0C : return ((crsrpos>>8)&0x03)|0xFC;
case 0xFF0D : return crsrpos&0xFF;
case 0xFF10 : return Ram[0xFF10] | 0x7C;
case 0xFF12 : return Ram[0xFF12] | 0xC0;
case 0xFF0E :
case 0xFF0F :
case 0xFF11 :
case 0xFF13 :
return Ram[addr];
case 0xFF14 : return Ram[addr]|0x07; // lowest 3 bits are always high
case 0xFF15 : return ecol[0]|0x80; // The highest bit is not used and so always 1
case 0xFF16 : return ecol[1]|0x80; // A few games (Rockman) used it...
case 0xFF17 : return ecol[2]|0x80;
case 0xFF18 : return ecol[3]|0x80;
case 0xFF19 : return (framecol&0xFF)|0x80;
case 0xFF1A : return (CharacterPositionReload>>8)&0xFF;
case 0xFF1B : return CharacterPositionReload&0xFF;
case 0xFF1C : return (beamy>>8)|0xFE;
case 0xFF1D : return beamy&0xFF; /// 1-8. bit of the rasterline counter
case 0xFF1E : return ((98+beamx)<<1)%228; // raster column
case 0xFF1F : return 0x80|(crsrphase<<3)|VertSubCount;
default:
return mem_c000_ffff[addr&0x3FFF];
}
break;
case 0xFE:
//return tcbmbus->Read(addr);
//return 0xFE;
return protectedPlayerMemory[addr & 0xfff];
case 0xFD:
switch (addr>>4) {
case 0xFD0: // RS232
return 0xFD;
case 0xFD1: // User port, PIO & 256 RAM expansion
//return (tap->IsButtonPressed()<<2)^0xFF;
return 0xFF;
case 0xFD2: // Speech hardware
case 0xFD4: // SID Card
case 0xFD5:
if (sidCard) {
return sidCard->read(addr & 0x1f);
}
return 0xFD;
case 0xFD3:
return Ram[0xFD30];
}
return 0xFD;
case 0xFC:
return mem_fc00_fcff[addr&0x3FFF];
default:
return mem_c000_ffff[addr&0x3FFF];
}
}
//fprintf(stderr,"Unhandled read %04X\n", addr);
return 0;
}
void TED::UpdateSerialState(unsigned char portVal)
{
static unsigned char prevVal = 0xC0;
//if ((prevVal ^ portVal)&8)
// tap->SetTapeMotor(CycleCounter, portVal&8);
prevVal = portVal;
}
void TED::Write(unsigned int addr, unsigned char value)
{
switch (addr&0xF000) {
case 0x0000:
switch ( addr & 0xFFFF ) {
case 0:
//fprintf(stderr,"$00 write: %02X\n", value);
prddr = value & 0xDF;
UpdateSerialState(prddr&~prp);
return;
case 1:
//fprintf(stderr,"$01 write: %02X\n", value);
prp = value;
UpdateSerialState(prddr&~prp);
return;
default:
actram[addr&0xFFFF] = value;
}
return;
case 0x1000:
case 0x2000:
case 0x3000:
actram[addr&0xFFFF] = value;
return;
case 0xD000:
if (sidCard) {
sidCard->write(addr & 0x1f, value);
sidCard->setFrequency(1);
}
case 0x4000:
case 0x5000:
case 0x6000:
case 0x7000:
case 0x8000:
case 0x9000:
case 0xA000:
case 0xB000:
case 0xC000:
case 0xE000:
actram[addr&RAMMask] = value;
return;
case 0xF000:
switch ( addr >> 8 ) {
case 0xFF:
switch (addr) {
case 0xFF00 :
t1on=false; // Timer1 disabled
t1start=(t1start & 0xFF00)|value;
timer1=(timer1 & 0xFF00)|value;
return;
case 0xFF01 :
t1on=true; // Timer1 enabled
t1start=(t1start & 0xFF)|(value<<8);
timer1=(timer1 & 0x00FF)|(value<<8);
return;
case 0xFF02 :
t2on=false; // Timer2 disabled
timer2=(timer2 & 0xFF00)|value;
return;
case 0xFF03 :
t2on=true; // Timer2 enabled
timer2=(timer2&0x00FF)|(value<<8);
return;
case 0xFF04 :
t3on=false; // Timer3 disabled
timer3=(timer3&0xFF00)|value;
return;
case 0xFF05 :
t3on=true; // Timer3 enabled
timer3=(timer3&0x00FF)|(value<<8);
return;
case 0xFF06 :
Ram[0xFF06]=value;
// get vertical offset of screen when smooth scroll
vshift = value&0x07;
// Check if screen is turned on
if (value&0x10 && beamy == 0 && !AttribFetch) {
AttribFetch = true;
VertSubCount = 7;
if (vshift != (beamy&7)) {
// FIXME: this is actually delayed by one cycle
if (beamx>4 && beamx<84)
ClockingState = TSS;
}
}
if ( (Ram[0xFF06] ^ value) & 0x1F) {
if (AttribFetch) {
if (vshift == (beamy&7)) {
BadLine |= 1;
if (beamx>=3 && beamx<89) {
unsigned char idleread = Read((cpuptr->getPC()+1)&0xFFFF);
unsigned int delay = (BadLine & 2) ? 0 : (beamx - 1) >> 1;
unsigned int invalidcount = (delay > 3) ? 3 : delay;
unsigned int invalidpos = delay - invalidcount;
invalidcount = (invalidcount < 40-invalidpos) ? invalidcount : 40-invalidpos;
unsigned int newdmapos = (invalidpos+invalidcount < 40) ? invalidpos+invalidcount : 40;
unsigned int newdmacount = 40 - newdmapos ;
unsigned int oldcount = 40 - newdmacount - invalidcount;
memcpy( DMAbuf, clrbuf, oldcount);
memset( DMAbuf+oldcount, idleread, invalidcount);
memcpy( DMAbuf+oldcount+invalidcount, VideoBase + CharacterCount + oldcount
+ invalidcount, newdmacount);
//DoDMA(tmpClrbuf + delay, 0);
ClockingState = THALT1;
} else if (beamx<111 && beamx>=89) {
// FIXME this breaks on FF1E writes
// swap DMA fetch pointers for colour DMA...
if (!(BadLine & 1) && beamx < 94) {
unsigned char *tmpbuf = clrbuf;
clrbuf = tmpClrbuf;
tmpClrbuf = tmpbuf;
}
//BadLine |= 1;
}
} else if (BadLine & 1 /*beamx>=91*/) {
if (beamx>=94) {
BadLine &= ~1;
} else if (beamx >= 91) {
unsigned char *tmpbuf = clrbuf;
clrbuf = tmpClrbuf;
tmpClrbuf = tmpbuf;
BadLine &= ~1;
}
}
}
}
// Delayed DMA?
// check for flat screen (23 rows)
fltscr = !(value&0x08);
// check for extended mode
ecmode=value&EXTCOLOR;
if (ecmode) {
tmp=(0xF800)&RAMMask;
scrattr|=EXTCOLOR;
} else {
tmp=(0xFC00)&RAMMask;
scrattr&=~EXTCOLOR;
}
charrambank=Ram+((Ram[0xFF13]<<8)&tmp);
charrombank=&(RomHi[0][((Ram[0xFF13] & 0x3C)<<8)&tmp]);
// check for graphics mode (5th b14it)
scrattr=(scrattr&~GRAPHMODE)|(value&GRAPHMODE);
return;
case 0xFF07 :
Ram[0xFF07]=value;
// check for narrow screen (38 columns)
nrwscr=value&0x08;
// get horizontal offset of screen when smooth scroll
hshift=value&0x07;
// check for reversed mode
rvsmode=value&0x80;
if (rvsmode) {
tmp=(0xF800)&RAMMask;
scrattr|=REVERSE;
} else {
tmp=(0xFC00)&RAMMask;
scrattr&=~REVERSE;
}
charrombank=&(RomHi[0][((Ram[0xFF13] & 0x3C)<<8)&tmp]);
charrambank=Ram+((Ram[0xFF13]<<8)&tmp);
cset = charrom ? charrombank : charrambank;
// check for multicolor mode
scrattr=(scrattr&~MULTICOLOR)|(value&0x10);
return;
case 0xFF08 :
Ram[0xFF08] = value;
return;
case 0xFF09 :
// clear the interrupt requester bits
// by writing 1 into them (!!)
Ram[0xFF09]=(Ram[0xFF09]&0x7F)&(~value);
return;
case 0xFF0A :
{
Ram[0xFF0A]=value;
// change the raster irq line
unsigned int newirqline = (irqline&0xFF)|((value&0x01)<<8);
if (newirqline != irqline) {
if (beamy == newirqline)
Ram[0xFF0A]&0x02 ? Ram[0xFF09]|=0x82 : Ram[0xFF09]|=0x02;
irqline = newirqline;
}
}
return;
case 0xFF0B :
{
Ram[0xFF0B]=value;
unsigned int newirqline = value|(irqline&0x0100);
if (newirqline != irqline) {
if (beamy == newirqline)
Ram[0xFF0A]&0x02 ? Ram[0xFF09]|=0x82 : Ram[0xFF09]|=0x02;
irqline = newirqline;
}
}
return;
case 0xFF0C :
crsrpos=((value<<8)|(crsrpos&0xFF))&0x3FF;
return;
case 0xFF0D :
crsrpos=value|(crsrpos&0xFF00);
return;
case 0xFF0E :
writeSoundReg(0, value);
Ram[0xFF0E]=value;
return;
case 0xFF0F :
writeSoundReg(1, value);
Ram[0xFF0F]=value;
return;
case 0xFF10 :
writeSoundReg(2, value & 3);
Ram[0xFF10]=value;
return;
case 0xFF11 :
Ram[0xFF11]=value;
writeSoundReg(3, value);
return;
case 0xFF12:
grbank=Ram+((value&0x38)<<10);
if ((value ^ Ram[0xFF12]) & 3)
writeSoundReg(4, value & 3);
// if the 2nd bit is set the chars are read from ROM
charrom=(value&0x04)>0;
if (charrom && Ram[0xFF13]<0x80)
scrattr|=ILLEGAL;
else {
scrattr&=~ILLEGAL;
cset = (charrom) ? charrombank : charrambank;
}
Ram[0xFF12]=value;
return;
case 0xFF13 :
// the 0th bit is not writable, it indicates if the ROMs are on
Ram[0xFF13]=(value&0xFE)|(Ram[0xFF13]&0x01);
// bit 1 is the fast/slow mode switch
fastmode = !(value&0x02);
(ecmode || rvsmode) ? tmp=(0xF800)&RAMMask : tmp=(0xFC00)&RAMMask;
charbank = ((value)<<8)&tmp;
charrambank=Ram+charbank;
charrombank=&(RomHi[0][charbank & 0x3C00]);
if (charrom && value<0x80)
scrattr|=ILLEGAL;
else {
scrattr&=~ILLEGAL;
(charrom) ? cset = charrombank : cset = charrambank;
}
return;
case 0xFF14 :
Ram[0xFF14]=value;
VideoBase=colorbank=Ram+(((value&0xF8)<<8)&RAMMask);
return;
case 0xFF15 :
ecol[0]=bmmcol[0]=mcol[0]=value&0x7F;
return;
case 0xFF16 :
ecol[1]=bmmcol[3]=mcol[1]=value&0x7F;
return;
case 0xFF17 :
ecol[2]=mcol[2]=value&0x7F;
return;
case 0xFF18 :
ecol[3]=value&0x7F;
return;
case 0xFF19 :
value &= 0x7F;
framecol=(value<<24)|(value<<16)|(value<<8)|value;
return;
case 0xFF1A :
CharacterPositionReload = (CharacterPositionReload & 0xFF) | ((value&3)<<8);
return;
case 0xFF1B :
CharacterPositionReload = (CharacterPositionReload & 0x300) | value;
return;
case 0xFF1C :
beamy=((value&0x01)<<8)|(beamy&0xFF);
return;
case 0xFF1D :
beamy=(beamy&0x0100)|value;
return;
case 0xFF1E :
{
unsigned int low_x = beamx&1;
// lowest 2 bits are not writable
// inverted value must be written
unsigned int new_beamx=((~value))&0xFC;
new_beamx >>= 1;
new_beamx>=98 ? new_beamx -= 98 : new_beamx += 16;
// writes are aligned to single clock cycles
if (low_x) {
aligned_write = true;
aw_addr_ptr = (unsigned char*)(&beamx);
aw_value = new_beamx;
} else {
beamx = new_beamx;
}
}
return;
case 0xFF1F :
VertSubCount=value&0x07;
crsrphase=(value&0x78)>>3;
return;
case 0xFF3E :
Ram[0xFF13]|=0x01;
RAMenable=false;
ChangeMemBankSetup(RAMenable);
return;
case 0xFF3F :
Ram[0xFF13]&=0xFE;
RAMenable=true;
ChangeMemBankSetup(RAMenable);
return;
}
actram[addr&RAMMask] = value;
return;
case 0xFE:
//tcbmbus->Write(addr,value);
return;
case 0xFD:
switch (addr>>4) {
case 0xFD0: // RS232
case 0xFD1: // User port, PIO & 256 RAM expansion
case 0xFD2: // Speech hardware
return;
case 0xFD3:
Ram[0xFD30] = value;
return;
case 0xFD4: // SID Card
case 0xFD5:
if (sidCard) {
sidCard->setFrequency(0);
sidCard->write(addr & 0x1f, value);
}
return;
case 0xFDD:
actromlo=&(RomLo[addr&0x03][0]);
actromhi=&(RomHi[(addr&0x0c)>>2][0]);
return;
}
return;
default:
actram[addr&RAMMask] = value;
return;
}
}
return;
}
void TED::dump(void *img)
{
// this is ugly :-P
fwrite(Ram,RAMSIZE,1,(FILE *) img);
fwrite(&RAMenable,sizeof(RAMenable),1,(FILE *) img);
fwrite(&t1start,sizeof(t1start),1, (FILE *) img);
fwrite(&t1on,sizeof(t1on),1, (FILE *) img);
fwrite(&t2on,sizeof(t2on),1, (FILE *) img);
fwrite(&t3on,sizeof(t3on),1, (FILE *) img);
fwrite(&timer1,sizeof(timer1),1, (FILE *) img);
fwrite(&timer2,sizeof(timer2),1, (FILE *) img);
fwrite(&timer3,sizeof(timer3),1, (FILE *) img);
fwrite(&beamx,sizeof(beamx),1, (FILE *) img);
fwrite(&beamy,sizeof(beamy),1, (FILE *) img);
//fwrite(&x,sizeof(x),1, (FILE *) img);
fwrite(&irqline,sizeof(irqline),1, (FILE *) img);
fwrite(&crsrpos,sizeof(crsrpos),1, (FILE *) img);
fwrite(&scrattr,sizeof(scrattr),1, (FILE *) img);
fwrite(&tmp,sizeof(tmp),1, (FILE *) img);
fwrite(&nrwscr,sizeof(nrwscr),1, (FILE *) img);
fwrite(&hshift,sizeof(hshift),1, (FILE *) img);
fwrite(&vshift,sizeof(vshift),1, (FILE *) img);
fwrite(&fltscr,sizeof(fltscr),1, (FILE *) img);
fwrite(&mcol,sizeof(mcol),1, (FILE *) img);
fwrite(chrbuf,40,1, (FILE *) img);
fwrite(clrbuf,40,1, (FILE *) img);
fwrite(&charrom,sizeof(charrom),1, (FILE *) img);
fwrite(&charbank,sizeof(charbank),1, (FILE *) img);
// fwrite(&TEDfreq1,sizeof(TEDfreq1),1, (FILE *) img);
// fwrite(&TEDfreq2,sizeof(TEDfreq2),1, (FILE *) img);
// fwrite(&TEDVolume,sizeof(TEDVolume),1, (FILE *) img);
// fwrite(&TEDDA,sizeof(TEDDA),1, (FILE *) img);
fwrite(&framecol,sizeof(framecol),1, (FILE *) img);
}
void TED::memin(void *img)
{
// this is ugly :-P
fread(Ram,RAMSIZE,1,(FILE *) img);
fread(&RAMenable,sizeof(RAMenable),1,(FILE *) img);
fread(&t1start,sizeof(t1start),1, (FILE *) img);
fread(&t1on,sizeof(t1on),1, (FILE *) img);
fread(&t2on,sizeof(t2on),1, (FILE *) img);
fread(&t3on,sizeof(t3on),1, (FILE *) img);
fread(&timer1,sizeof(timer1),1, (FILE *) img);
fread(&timer2,sizeof(timer2),1, (FILE *) img);
fread(&timer3,sizeof(timer3),1, (FILE *) img);
fread(&beamx,sizeof(beamx),1, (FILE *) img);
fread(&beamy,sizeof(beamy),1, (FILE *) img);
//fread(&x,sizeof(x),1, (FILE *) img);
fread(&irqline,sizeof(irqline),1, (FILE *) img);
fread(&crsrpos,sizeof(crsrpos),1, (FILE *) img);
fread(&scrattr,sizeof(scrattr),1, (FILE *) img);
fread(&tmp,sizeof(tmp),1, (FILE *) img);
fread(&nrwscr,sizeof(nrwscr),1, (FILE *) img);
fread(&hshift,sizeof(hshift),1, (FILE *) img);
fread(&vshift,sizeof(vshift),1, (FILE *) img);
fread(&fltscr,sizeof(fltscr),1, (FILE *) img);
fread(&mcol,sizeof(mcol),1, (FILE *) img);
fread(chrbuf,40,1, (FILE *) img);
fread(clrbuf,40,1, (FILE *) img);
fread(&charrom,sizeof(charrom),1, (FILE *) img);
fread(&charbank,sizeof(charbank),1, (FILE *) img);
// fread(&TEDfreq1,sizeof(TEDfreq1),1, (FILE *) img);
// fread(&TEDfreq2,sizeof(TEDfreq2),1, (FILE *) img);
// fread(&TEDVolume,sizeof(TEDVolume),1, (FILE *) img);
// fread(&TEDDA,sizeof(TEDDA),1, (FILE *) img);
fread(&framecol,sizeof(framecol),1, (FILE *) img);
for (int i=0; i<5; i++)
writeSoundReg(i, Ram[0xFF0E + i]);
beamy=0;
beamx=0;
scrptr=&(screen[0]);
charrambank=Ram+charbank;
charrombank=&(RomHi[0][charbank & 0x3C00]);
(charrom) ? cset = charrombank : cset = charrambank;
}
// when multi and extended color modes are all on the screen is blank
inline void TED::mcec()
{
memset( scrptr, 0, 8);
}
// renders hires text with reverse (128 chars)
inline void TED::hi_text()
{
unsigned char chr;
unsigned char charcol;
unsigned char mask;
unsigned char *wbuffer = scrptr + hshift;
// get the actual physical character column
charcol=clrbuf[x];
chr=chrbuf[x];
if ((charcol)&0x80 && !crsrblinkon)
mask = 00;
else
mask = cset[((chr&0x7F)<<3)|VertSubCount];
if (chr&0x80)
mask ^= 0xFF;
if (crsrpos==((CharacterPosition+x)&0x3FF) && crsrblinkon )
mask ^= 0xFF;
wbuffer[0] = (mask & 0x80) ? charcol : mcol[0];
wbuffer[1] = (mask & 0x40) ? charcol : mcol[0];
wbuffer[2] = (mask & 0x20) ? charcol : mcol[0];
wbuffer[3] = (mask & 0x10) ? charcol : mcol[0];
wbuffer[4] = (mask & 0x08) ? charcol : mcol[0];
wbuffer[5] = (mask & 0x04) ? charcol : mcol[0];
wbuffer[6] = (mask & 0x02) ? charcol : mcol[0];
wbuffer[7] = (mask & 0x01) ? charcol : mcol[0];
}
// renders text without the reverse (all 256 chars)
inline void TED::rv_text()
{
unsigned char chr;
unsigned char charcol;
unsigned char mask;
unsigned char *wbuffer = scrptr + hshift;
// get the actual physical character column
charcol=clrbuf[x];
chr=chrbuf[x];
if ((charcol)&0x80 && !crsrblinkon)
mask = 00;
else
mask = cset[(chr<<3)|VertSubCount];
if (crsrpos==((CharacterPosition+x)&0x3FF) && crsrblinkon )
mask ^= 0xFF;
wbuffer[0] = (mask & 0x80) ? charcol : mcol[0];
wbuffer[1] = (mask & 0x40) ? charcol : mcol[0];
wbuffer[2] = (mask & 0x20) ? charcol : mcol[0];
wbuffer[3] = (mask & 0x10) ? charcol : mcol[0];
wbuffer[4] = (mask & 0x08) ? charcol : mcol[0];
wbuffer[5] = (mask & 0x04) ? charcol : mcol[0];
wbuffer[6] = (mask & 0x02) ? charcol : mcol[0];
wbuffer[7] = (mask & 0x01) ? charcol : mcol[0];
}
// renders extended color text
inline void TED::ec_text()
{
unsigned char charcol;
unsigned char chr;
unsigned char mask;
unsigned char *wbuffer = scrptr + hshift;
// get the actual physical character column
charcol = clrbuf[x];
chr = chrbuf[x];
mask = cset[ ((chr&0x3F)<<3)|VertSubCount ];
chr = (chr&0xC0)>>6;
wbuffer[0] = (mask & 0x80) ? charcol : ecol[chr];
wbuffer[1] = (mask & 0x40) ? charcol : ecol[chr];
wbuffer[2] = (mask & 0x20) ? charcol : ecol[chr];
wbuffer[3] = (mask & 0x10) ? charcol : ecol[chr];
wbuffer[4] = (mask & 0x08) ? charcol : ecol[chr];
wbuffer[5] = (mask & 0x04) ? charcol : ecol[chr];
wbuffer[6] = (mask & 0x02) ? charcol : ecol[chr];
wbuffer[7] = (mask & 0x01) ? charcol : ecol[chr];
}
// renders multicolor text
inline void TED::mc_text_rvs()
{
unsigned char chr=chrbuf[x];
unsigned char charcol=clrbuf[x];
unsigned char *wbuffer = scrptr + hshift;
unsigned char mask;
mask = cset[ (chr<<3)|VertSubCount ];
if (charcol&0x08) { // if character is multicolored
mcol[3]=charcol&0xF7;
wbuffer[0] = wbuffer[1] = mcol[ (mask & 0xC0) >> 6 ];
wbuffer[2] = wbuffer[3] = mcol[ (mask & 0x30) >> 4 ];
wbuffer[4] = wbuffer[5] = mcol[ (mask & 0x0C) >> 2 ];
wbuffer[6] = wbuffer[7] = mcol[ mask & 0x03 ];
} else { // this is a normally colored character
wbuffer[0] = (mask & 0x80) ? charcol : mcol[0];
wbuffer[1] = (mask & 0x40) ? charcol : mcol[0];
wbuffer[2] = (mask & 0x20) ? charcol : mcol[0];
wbuffer[3] = (mask & 0x10) ? charcol : mcol[0];
wbuffer[4] = (mask & 0x08) ? charcol : mcol[0];
wbuffer[5] = (mask & 0x04) ? charcol : mcol[0];
wbuffer[6] = (mask & 0x02) ? charcol : mcol[0];
wbuffer[7] = (mask & 0x01) ? charcol : mcol[0];
}
}
// renders multicolor text with reverse bit set
inline void TED::mc_text()
{
unsigned char charcol=clrbuf[x];
unsigned char chr=chrbuf[x]&0x7F;
unsigned char *wbuffer = scrptr + hshift;
unsigned char mask;
mask = cset[ (chr<<3)|VertSubCount ];
if ((charcol)&0x08) { // if character is multicolored
mcol[3]=(charcol)&0xF7;
wbuffer[0] = wbuffer[1] = mcol[ (mask & 0xC0) >> 6 ];
wbuffer[2] = wbuffer[3] = mcol[ (mask & 0x30) >> 4 ];
wbuffer[4] = wbuffer[5] = mcol[ (mask & 0x0C) >> 2 ];
wbuffer[6] = wbuffer[7] = mcol[ mask & 0x03 ];
} else { // this is a normally colored character
wbuffer[0] = (mask & 0x80) ? charcol : mcol[0];
wbuffer[1] = (mask & 0x40) ? charcol : mcol[0];
wbuffer[2] = (mask & 0x20) ? charcol : mcol[0];
wbuffer[3] = (mask & 0x10) ? charcol : mcol[0];
wbuffer[4] = (mask & 0x08) ? charcol : mcol[0];
wbuffer[5] = (mask & 0x04) ? charcol : mcol[0];
wbuffer[6] = (mask & 0x02) ? charcol : mcol[0];
wbuffer[7] = (mask & 0x01) ? charcol : mcol[0];
}
}
// renders hires bitmap graphics
inline void TED::hi_bitmap()
{
unsigned char mask;
unsigned char *wbuffer = scrptr + hshift;
// get the actual color attributes
hcol[0]=(chrbuf[x]&0x0F)|(clrbuf[x]&0x70);
hcol[1]=((chrbuf[x]&0xF0)>>4)|((clrbuf[x]&0x07)<<4);
mask = grbank[ (((CharacterPosition+x)<<3)&0x1FFF)|VertSubCount ];
wbuffer[0] = (mask & 0x80) ? hcol[1] : hcol[0];
wbuffer[1] = (mask & 0x40) ? hcol[1] : hcol[0];
wbuffer[2] = (mask & 0x20) ? hcol[1] : hcol[0];
wbuffer[3] = (mask & 0x10) ? hcol[1] : hcol[0];
wbuffer[4] = (mask & 0x08) ? hcol[1] : hcol[0];
wbuffer[5] = (mask & 0x04) ? hcol[1] : hcol[0];
wbuffer[6] = (mask & 0x02) ? hcol[1] : hcol[0];