2 // mmu.cpp: Memory management
5 // (C) 2013 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 09/27/2013 Created this file
16 #include "applevideo.h"
25 // Address Map enumeration
26 enum { AM_RAM, AM_ROM, AM_BANKED, AM_READ, AM_WRITE, AM_READ_WRITE, AM_END_OF_LIST };
28 // Macros for function pointers
29 // The typedef would be something like:
30 //typedef ReadFunc (uint8_t (*)(uint16_t));
31 //typedef WriteFunc (void (*)(uint16_t, uint8_t));
32 #define READFUNC(x) uint8_t (* x)(uint16_t)
33 #define WRITEFUNC(x) void (* x)(uint16_t, uint8_t)
36 uint8_t ** addrPtrRead[0x10000];
37 uint8_t ** addrPtrWrite[0x10000];
38 uint16_t addrOffset[0x10000];
40 READFUNC(funcMapRead[0x10000]);
41 WRITEFUNC(funcMapWrite[0x10000]);
55 // Dunno if I like this approach or not...
57 // AM_RANGE(0x0000, 0xBFFF) AM_RAM AM_BASE(ram) AM_SHARE(1)
58 // AM_RANGE(0xC000, 0xC001) AM_READWRITE(readFunc, writeFunc)
61 // Would need a pointer for 80STORE as well...
63 uint8_t * pageZeroMemory = &ram[0x0000]; // $0000 - $01FF
64 uint8_t * mainMemoryR = &ram[0x0200]; // $0200 - $BFFF (read)
65 uint8_t * mainMemoryW = &ram[0x0200]; // $0200 - $BFFF (write)
67 uint8_t * mainMemoryTextR = &ram[0x0400]; // $0400 - $07FF (read)
68 uint8_t * mainMemoryTextW = &ram[0x0400]; // $0400 - $07FF (write)
69 uint8_t * mainMemoryHGRR = &ram[0x2000]; // $2000 - $3FFF (read)
70 uint8_t * mainMemoryHGRW = &ram[0x2000]; // $2000 - $3FFF (write)
72 uint8_t * slotMemory = &rom[0xC100]; // $C100 - $CFFF
73 uint8_t * slot3Memory = &rom[0xC300]; // $C300 - $C3FF
74 uint8_t * slot6Memory = &diskROM[0]; // $C600 - $C6FF
75 uint8_t * lcBankMemoryR = &ram[0xD000]; // $D000 - $DFFF (read)
76 uint8_t * lcBankMemoryW = &ram[0xD000]; // $D000 - $DFFF (write)
77 uint8_t * upperMemoryR = &ram[0xE000]; // $E000 - $FFFF (read)
78 uint8_t * upperMemoryW = &ram[0xE000]; // $E000 - $FFFF (write)
81 // Function prototypes
82 uint8_t ReadNOP(uint16_t);
83 void WriteNOP(uint16_t, uint8_t);
84 uint8_t ReadMemory(uint16_t);
85 void WriteMemory(uint16_t, uint8_t);
86 uint8_t ReadKeyboard(uint16_t);
87 void Switch80STORE(uint16_t, uint8_t);
88 void SwitchRAMRD(uint16_t, uint8_t);
89 void SwitchRAMWRT(uint16_t, uint8_t);
90 void SwitchSLOTCXROM(uint16_t, uint8_t);
91 void SwitchALTZP(uint16_t, uint8_t);
92 void SwitchSLOTC3ROM(uint16_t, uint8_t);
93 void Switch80COL(uint16_t, uint8_t);
94 void SwitchALTCHARSET(uint16_t, uint8_t);
95 uint8_t ReadKeyStrobe(uint16_t);
96 uint8_t ReadBANK2(uint16_t);
97 uint8_t ReadLCRAM(uint16_t);
98 uint8_t ReadRAMRD(uint16_t);
99 uint8_t ReadRAMWRT(uint16_t);
100 uint8_t ReadSLOTCXROM(uint16_t);
101 uint8_t ReadALTZP(uint16_t);
102 uint8_t ReadSLOTC3ROM(uint16_t);
103 uint8_t Read80STORE(uint16_t);
104 uint8_t ReadVBL(uint16_t);
105 uint8_t ReadTEXT(uint16_t);
106 uint8_t ReadMIXED(uint16_t);
107 uint8_t ReadPAGE2(uint16_t);
108 uint8_t ReadHIRES(uint16_t);
109 uint8_t ReadALTCHARSET(uint16_t);
110 uint8_t Read80COL(uint16_t);
111 void WriteKeyStrobe(uint16_t, uint8_t);
112 uint8_t ReadSpeaker(uint16_t);
113 void WriteSpeaker(uint16_t, uint8_t);
114 uint8_t SwitchLCR(uint16_t);
115 void SwitchLCW(uint16_t, uint8_t);
117 uint8_t SwitchTEXTR(uint16_t);
118 void SwitchTEXTW(uint16_t, uint8_t);
119 uint8_t SwitchMIXEDR(uint16_t);
120 void SwitchMIXEDW(uint16_t, uint8_t);
121 uint8_t SwitchPAGE2R(uint16_t);
122 void SwitchPAGE2W(uint16_t, uint8_t);
123 uint8_t SwitchHIRESR(uint16_t);
124 void SwitchHIRESW(uint16_t, uint8_t);
125 uint8_t SwitchDHIRESR(uint16_t);
126 void SwitchDHIRESW(uint16_t, uint8_t);
127 void SwitchIOUDIS(uint16_t, uint8_t);
128 uint8_t Slot6R(uint16_t);
129 void Slot6W(uint16_t, uint8_t);
130 void HandleSlot6(uint16_t, uint8_t);
131 uint8_t ReadButton0(uint16_t);
132 uint8_t ReadButton1(uint16_t);
133 uint8_t ReadPaddle0(uint16_t);
134 uint8_t ReadIOUDIS(uint16_t);
135 uint8_t ReadDHIRES(uint16_t);
136 //uint8_t SwitchR(uint16_t);
137 //void SwitchW(uint16_t, uint8_t);
140 // The main Apple //e memory map
141 AddressMap memoryMap[] = {
142 { 0x0000, 0x01FF, AM_RAM, &pageZeroMemory, 0, 0, 0 },
143 { 0x0200, 0xBFFF, AM_BANKED, &mainMemoryR, &mainMemoryW, 0, 0 },
145 // These will overlay over the previously written memory accessors
146 { 0x0400, 0x07FF, AM_BANKED, &mainMemoryTextR, &mainMemoryTextW, 0, 0 },
147 { 0x2000, 0x3FFF, AM_BANKED, &mainMemoryHGRR, &mainMemoryHGRW, 0, 0 },
149 { 0xC000, 0xC001, AM_READ_WRITE, 0, 0, ReadKeyboard, Switch80STORE },
150 { 0xC002, 0xC003, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchRAMRD },
151 { 0xC004, 0xC005, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchRAMWRT },
152 { 0xC006, 0xC007, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchSLOTCXROM },
153 { 0xC008, 0xC009, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchALTZP },
154 { 0xC00A, 0xC00B, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchSLOTC3ROM },
155 { 0xC00C, 0xC00D, AM_READ_WRITE, 0, 0, ReadKeyboard, Switch80COL },
156 { 0xC00E, 0xC00F, AM_READ_WRITE, 0, 0, ReadKeyboard, SwitchALTCHARSET },
157 { 0xC010, 0xC010, AM_READ_WRITE, 0, 0, ReadKeyStrobe, WriteKeyStrobe },
158 { 0xC011, 0xC011, AM_READ_WRITE, 0, 0, ReadBANK2, WriteKeyStrobe },
159 { 0xC012, 0xC012, AM_READ_WRITE, 0, 0, ReadLCRAM, WriteKeyStrobe },
160 { 0xC013, 0xC013, AM_READ_WRITE, 0, 0, ReadRAMRD, WriteKeyStrobe },
161 { 0xC014, 0xC014, AM_READ_WRITE, 0, 0, ReadRAMWRT, WriteKeyStrobe },
162 { 0xC015, 0xC015, AM_READ_WRITE, 0, 0, ReadSLOTCXROM, WriteKeyStrobe },
163 { 0xC016, 0xC016, AM_READ_WRITE, 0, 0, ReadALTZP, WriteKeyStrobe },
164 { 0xC017, 0xC017, AM_READ_WRITE, 0, 0, ReadSLOTC3ROM, WriteKeyStrobe },
165 { 0xC018, 0xC018, AM_READ_WRITE, 0, 0, Read80STORE, WriteKeyStrobe },
166 { 0xC019, 0xC019, AM_READ_WRITE, 0, 0, ReadVBL, WriteKeyStrobe },
167 { 0xC01A, 0xC01A, AM_READ_WRITE, 0, 0, ReadTEXT, WriteKeyStrobe },
168 { 0xC01B, 0xC01B, AM_READ_WRITE, 0, 0, ReadMIXED, WriteKeyStrobe },
169 { 0xC01C, 0xC01C, AM_READ_WRITE, 0, 0, ReadPAGE2, WriteKeyStrobe },
170 { 0xC01D, 0xC01D, AM_READ_WRITE, 0, 0, ReadHIRES, WriteKeyStrobe },
171 { 0xC01E, 0xC01E, AM_READ_WRITE, 0, 0, ReadALTCHARSET, WriteKeyStrobe },
172 { 0xC01F, 0xC01F, AM_READ_WRITE, 0, 0, Read80COL, WriteKeyStrobe },
173 { 0xC030, 0xC03F, AM_READ_WRITE, 0, 0, ReadSpeaker, WriteSpeaker },
174 { 0xC050, 0xC051, AM_READ_WRITE, 0, 0, SwitchTEXTR, SwitchTEXTW },
175 { 0xC052, 0xC053, AM_READ_WRITE, 0, 0, SwitchMIXEDR, SwitchMIXEDW },
176 { 0xC054, 0xC055, AM_READ_WRITE, 0, 0, SwitchPAGE2R, SwitchPAGE2W },
177 { 0xC056, 0xC057, AM_READ_WRITE, 0, 0, SwitchHIRESR, SwitchHIRESW },
178 { 0xC05E, 0xC05F, AM_READ_WRITE, 0, 0, SwitchDHIRESR, SwitchDHIRESW },
179 { 0xC061, 0xC061, AM_READ, 0, 0, ReadButton0, 0 },
180 { 0xC062, 0xC062, AM_READ, 0, 0, ReadButton1, 0 },
181 { 0xC064, 0xC067, AM_READ, 0, 0, ReadPaddle0, 0 },
182 // { 0xC07E, 0xC07F, AM_READ_WRITE, 0, 0, SwitchIOUDISR, SwitchIOUDISW },
183 { 0xC07E, 0xC07E, AM_READ_WRITE, 0, 0, ReadIOUDIS, SwitchIOUDIS },
184 { 0xC07F, 0xC07F, AM_READ_WRITE, 0, 0, ReadDHIRES, SwitchIOUDIS },
185 { 0xC080, 0xC08F, AM_READ_WRITE, 0, 0, SwitchLCR, SwitchLCW },
186 { 0xC0E0, 0xC0EF, AM_READ_WRITE, 0, 0, Slot6R, Slot6W },
187 { 0xC100, 0xCFFF, AM_ROM, &slotMemory, 0, 0, 0 },
189 // This will overlay the slotMemory accessors for slot 6 ROM
190 { 0xC300, 0xC3FF, AM_ROM, &slot3Memory, 0, 0, 0 },
191 { 0xC600, 0xC6FF, AM_ROM, &slot6Memory, 0, 0, 0 },
193 { 0xD000, 0xDFFF, AM_BANKED, &lcBankMemoryR, &lcBankMemoryW, 0, 0 },
194 { 0xE000, 0xFFFF, AM_BANKED, &upperMemoryR, &upperMemoryW, 0, 0 },
195 { 0x0000, 0x0000, AM_END_OF_LIST, 0, 0, 0, 0 }
199 void SetupAddressMap(void)
201 for(uint32_t i=0; i<0x10000; i++)
203 funcMapRead[i] = ReadNOP;
204 funcMapWrite[i] = WriteNOP;
212 while (memoryMap[i].type != AM_END_OF_LIST)
214 switch (memoryMap[i].type)
217 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
219 funcMapRead[j] = ReadMemory;
220 funcMapWrite[j] = WriteMemory;
221 addrPtrRead[j] = memoryMap[i].memory;
222 addrPtrWrite[j] = memoryMap[i].memory;
223 addrOffset[j] = j - memoryMap[i].start;
224 //WriteLog("SetupAddressMap: j=$%04X, addrOffset[j]=$%04X\n", j, addrOffset[j]);
229 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
231 funcMapRead[j] = ReadMemory;
232 addrPtrRead[j] = memoryMap[i].memory;
233 addrOffset[j] = j - memoryMap[i].start;
238 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
240 funcMapRead[j] = ReadMemory;
241 funcMapWrite[j] = WriteMemory;
242 addrPtrRead[j] = memoryMap[i].memory;
243 addrPtrWrite[j] = memoryMap[i].altMemory;
244 addrOffset[j] = j - memoryMap[i].start;
249 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
250 funcMapRead[j] = memoryMap[i].read;
254 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
255 funcMapWrite[j] = memoryMap[i].write;
259 for(uint32_t j=memoryMap[i].start; j<=memoryMap[i].end; j++)
261 funcMapRead[j] = memoryMap[i].read;
262 funcMapWrite[j] = memoryMap[i].write;
271 // This should correctly set up the LC pointers, but it doesn't
272 // for some reason... :-/
273 // It's because we were storing pointers directly, instead of pointers
274 // to the pointer... It's complicated. :-)
280 // Built-in functions
282 uint8_t ReadNOP(uint16_t)
288 void WriteNOP(uint16_t, uint8_t)
293 uint8_t ReadMemory(uint16_t address)
295 //WriteLog("ReadMemory: addr=$%04X, addrPtrRead[addr]=$%X, addrOffset[addr]=$%X, val=$%02X\n", address, addrPtrRead[address], addrOffset[address], addrPtrRead[address][addrOffset[address]]);
296 // We are guaranteed a valid address here by the setup function, so there's
297 // no need to do any checking here.
298 return (*addrPtrRead[address])[addrOffset[address]];
302 void WriteMemory(uint16_t address, uint8_t byte)
304 // We can write protect memory this way, but it adds a branch to the mix. :-/
305 // (this can be avoided by setting up another bank of memory which we
307 if ((*addrPtrWrite[address]) == 0)
310 (*addrPtrWrite[address])[addrOffset[address]] = byte;
315 // The main memory access functions used by V65C02
317 uint8_t AppleReadMem(uint16_t address)
319 return (*(funcMapRead[address]))(address);
323 void AppleWriteMem(uint16_t address, uint8_t byte)
325 (*(funcMapWrite[address]))(address, byte);
330 // Actual emulated I/O functions follow
332 uint8_t ReadKeyboard(uint16_t /*addr*/)
334 return lastKeyPressed | ((uint8_t)keyDown << 7);
338 void Switch80STORE(uint16_t address, uint8_t)
340 store80Mode = (bool)(address & 0x01);
341 WriteLog("Setting 80STORE to %s...\n", (store80Mode ? "ON" : "off"));
345 mainMemoryTextR = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
346 mainMemoryTextW = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
347 // mainMemoryHGRR = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
348 // mainMemoryHGRW = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
352 mainMemoryTextR = (ramwrt ? &ram2[0x0400] : &ram[0x0400]);
353 mainMemoryTextW = (ramwrt ? &ram2[0x0400] : &ram[0x0400]);
354 // mainMemoryHGRR = (ramwrt ? &ram2[0x2000] : &ram[0x2000]);
355 // mainMemoryHGRW = (ramwrt ? &ram2[0x2000] : &ram[0x2000]);
360 void SwitchRAMRD(uint16_t address, uint8_t)
362 ramrd = (bool)(address & 0x01);
363 mainMemoryR = (ramrd ? &ram2[0x0200] : &ram[0x0200]);
364 mainMemoryHGRR = (ramrd ? &ram2[0x2000] : &ram[0x2000]);
369 mainMemoryTextR = (ramrd ? &ram2[0x0400] : &ram[0x0400]);
373 void SwitchRAMWRT(uint16_t address, uint8_t)
375 ramwrt = (bool)(address & 0x01);
376 mainMemoryW = (ramwrt ? &ram2[0x0200] : &ram[0x0200]);
377 mainMemoryHGRW = (ramwrt ? &ram2[0x2000] : &ram[0x2000]);
382 mainMemoryTextW = (ramwrt ? &ram2[0x0400] : &ram[0x0400]);
386 void SwitchSLOTCXROM(uint16_t address, uint8_t)
388 //WriteLog("Setting SLOTCXROM to %s...\n", ((address & 0x01) ^ 0x01 ? "ON" : "off"));
389 // This is the only soft switch that breaks the usual convention.
390 slotCXROM = !((bool)(address & 0x01));
391 // slot3Memory = (slotCXROM ? &rom[0] : &rom[0xC300]);
392 slot6Memory = (slotCXROM ? &diskROM[0] : &rom[0xC600]);
396 void SwitchALTZP(uint16_t address, uint8_t)
398 altzp = (bool)(address & 0x01);
399 pageZeroMemory = (altzp ? &ram2[0x0000] : &ram[0x0000]);
403 //extern bool dumpDis;
405 void SwitchSLOTC3ROM(uint16_t address, uint8_t)
408 //WriteLog("Setting SLOTC3ROM to %s...\n", (address & 0x01 ? "ON" : "off"));
409 slotC3ROM = (bool)(address & 0x01);
410 // slotC3ROM = false;
411 // Seems the h/w forces this with an 80 column card in slot 3...
412 slot3Memory = (slotC3ROM ? &rom[0] : &rom[0xC300]);
413 // slot3Memory = &rom[0xC300];
417 void Switch80COL(uint16_t address, uint8_t)
419 col80Mode = (bool)(address & 0x01);
423 void SwitchALTCHARSET(uint16_t address, uint8_t)
425 alternateCharset = (bool)(address & 0x01);
429 uint8_t ReadKeyStrobe(uint16_t)
431 uint8_t byte = lastKeyPressed | ((uint8_t)keyDown << 7);
437 uint8_t ReadBANK2(uint16_t)
439 return (lcState < 0x04 ? 0x80 : 0x00);
443 uint8_t ReadLCRAM(uint16_t)
445 // If bits 0 & 1 are set, but not at the same time, then it's ROM
446 uint8_t lcROM = (lcState & 0x1) ^ ((lcState & 0x02) >> 1);
447 return (lcROM ? 0x00 : 0x80);
451 uint8_t ReadRAMRD(uint16_t)
453 return (uint8_t)ramrd << 7;
457 uint8_t ReadRAMWRT(uint16_t)
459 return (uint8_t)ramwrt << 7;
463 uint8_t ReadSLOTCXROM(uint16_t)
465 return (uint8_t)slotCXROM << 7;
469 uint8_t ReadALTZP(uint16_t)
471 return (uint8_t)altzp << 7;
475 uint8_t ReadSLOTC3ROM(uint16_t)
478 return (uint8_t)slotC3ROM << 7;
482 uint8_t Read80STORE(uint16_t)
484 return (uint8_t)store80Mode << 7;
488 uint8_t ReadVBL(uint16_t)
490 return (uint8_t)vbl << 7;
494 uint8_t ReadTEXT(uint16_t)
496 return (uint8_t)textMode << 7;
500 uint8_t ReadMIXED(uint16_t)
502 return (uint8_t)mixedMode << 7;
506 uint8_t ReadPAGE2(uint16_t)
508 return (uint8_t)displayPage2 << 7;
512 uint8_t ReadHIRES(uint16_t)
514 return (uint8_t)hiRes << 7;
518 uint8_t ReadALTCHARSET(uint16_t)
520 return (uint8_t)alternateCharset << 7;
524 uint8_t Read80COL(uint16_t)
526 return (uint8_t)col80Mode << 7;
530 void WriteKeyStrobe(uint16_t, uint8_t)
536 uint8_t ReadSpeaker(uint16_t)
543 void WriteSpeaker(uint16_t, uint8_t)
549 uint8_t SwitchLCR(uint16_t address)
551 lcState = address & 0x0B;
557 void SwitchLCW(uint16_t address, uint8_t)
559 lcState = address & 0x0B;
570 WriteLog("SwitchLC: Read RAM bank 2, no write\n");
572 // [R ] Read RAM bank 2; no write
573 lcBankMemoryR = (altzp ? &ram2[0xD000] : &ram[0xD000]);
575 upperMemoryR = (altzp ? &ram2[0xE000] : &ram[0xE000]);
580 WriteLog("SwitchLC: Read ROM, write bank 2\n");
582 // [RR] Read ROM; write RAM bank 2
583 lcBankMemoryR = &rom[0xD000];
584 lcBankMemoryW = (altzp ? &ram2[0xD000] : &ram[0xD000]);
585 upperMemoryR = &rom[0xE000];
586 upperMemoryW = (altzp ? &ram2[0xE000] : &ram[0xE000]);
590 WriteLog("SwitchLC: Read ROM, no write\n");
592 // [R ] Read ROM; no write
593 lcBankMemoryR = &rom[0xD000];
595 upperMemoryR = &rom[0xE000];
600 WriteLog("SwitchLC: Read/write bank 2\n");
602 // [RR] Read RAM bank 2; write RAM bank 2
603 lcBankMemoryR = (altzp ? &ram2[0xD000] : &ram[0xD000]);
604 lcBankMemoryW = (altzp ? &ram2[0xD000] : &ram[0xD000]);
605 upperMemoryR = (altzp ? &ram2[0xE000] : &ram[0xE000]);
606 upperMemoryW = (altzp ? &ram2[0xE000] : &ram[0xE000]);
609 // [R ] Read RAM bank 1; no write
610 lcBankMemoryR = (altzp ? &ram2[0xC000] : &ram[0xC000]);
612 upperMemoryR = (altzp ? &ram2[0xE000] : &ram[0xE000]);
616 // [RR] Read ROM; write RAM bank 1
617 lcBankMemoryR = &rom[0xD000];
618 lcBankMemoryW = (altzp ? &ram2[0xC000] : &ram[0xC000]);
619 upperMemoryR = &rom[0xE000];
620 upperMemoryW = (altzp ? &ram2[0xE000] : &ram[0xE000]);
623 // [R ] Read ROM; no write
624 lcBankMemoryR = &rom[0xD000];
626 upperMemoryR = &rom[0xE000];
630 // [RR] Read RAM bank 1; write RAM bank 1
631 lcBankMemoryR = (altzp ? &ram2[0xC000] : &ram[0xC000]);
632 lcBankMemoryW = (altzp ? &ram2[0xC000] : &ram[0xC000]);
633 upperMemoryR = (altzp ? &ram2[0xE000] : &ram[0xE000]);
634 upperMemoryW = (altzp ? &ram2[0xE000] : &ram[0xE000]);
640 uint8_t SwitchTEXTR(uint16_t address)
642 WriteLog("Setting TEXT to %s...\n", (address & 0x01 ? "ON" : "off"));
643 textMode = (bool)(address & 0x01);
648 void SwitchTEXTW(uint16_t address, uint8_t)
650 WriteLog("Setting TEXT to %s...\n", (address & 0x01 ? "ON" : "off"));
651 textMode = (bool)(address & 0x01);
655 uint8_t SwitchMIXEDR(uint16_t address)
657 WriteLog("Setting MIXED to %s...\n", (address & 0x01 ? "ON" : "off"));
658 mixedMode = (bool)(address & 0x01);
663 void SwitchMIXEDW(uint16_t address, uint8_t)
665 WriteLog("Setting MIXED to %s...\n", (address & 0x01 ? "ON" : "off"));
666 mixedMode = (bool)(address & 0x01);
670 uint8_t SwitchPAGE2R(uint16_t address)
672 WriteLog("Setting PAGE2 to %s...\n", (address & 0x01 ? "ON" : "off"));
673 displayPage2 = (bool)(address & 0x01);
677 mainMemoryTextR = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
678 mainMemoryTextW = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
679 // mainMemoryHGRR = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
680 // mainMemoryHGRW = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
687 void SwitchPAGE2W(uint16_t address, uint8_t)
689 WriteLog("Setting PAGE2 to %s...\n", (address & 0x01 ? "ON" : "off"));
690 displayPage2 = (bool)(address & 0x01);
694 mainMemoryTextR = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
695 mainMemoryTextW = (displayPage2 ? &ram2[0x0400] : &ram[0x0400]);
696 // mainMemoryHGRR = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
697 // mainMemoryHGRW = (displayPage2 ? &ram2[0x2000] : &ram[0x2000]);
702 uint8_t SwitchHIRESR(uint16_t address)
704 WriteLog("Setting HIRES to %s...\n", (address & 0x01 ? "ON" : "off"));
705 hiRes = (bool)(address & 0x01);
710 void SwitchHIRESW(uint16_t address, uint8_t)
712 WriteLog("Setting HIRES to %s...\n", (address & 0x01 ? "ON" : "off"));
713 hiRes = (bool)(address & 0x01);
717 uint8_t SwitchDHIRESR(uint16_t address)
719 WriteLog("Setting DHIRES to %s (ioudis = %s)...\n", ((address & 0x01) ^ 0x01 ? "ON" : "off"), (ioudis ? "ON" : "off"));
720 // Hmm, this breaks convention too, like SLOTCXROM
722 dhires = !((bool)(address & 0x01));
728 void SwitchDHIRESW(uint16_t address, uint8_t)
730 WriteLog("Setting DHIRES to %s (ioudis = %s)...\n", ((address & 0x01) ^ 0x01 ? "ON" : "off"), (ioudis ? "ON" : "off"));
732 dhires = !((bool)(address & 0x01));
736 void SwitchIOUDIS(uint16_t address, uint8_t)
738 ioudis = !((bool)(address & 0x01));
742 uint8_t Slot6R(uint16_t address)
744 //WriteLog("Slot6R: address = %X\n", address & 0x0F);
745 // HandleSlot6(address, 0);
747 uint8_t state = address & 0x0F;
759 floppyDrive.ControlStepper(state);
763 floppyDrive.ControlMotor(state & 0x01);
767 floppyDrive.DriveEnable(state & 0x01);
770 return floppyDrive.ReadWrite();
773 return floppyDrive.GetLatchValue();
776 floppyDrive.SetReadMode();
779 floppyDrive.SetWriteMode();
787 void Slot6W(uint16_t address, uint8_t byte)
789 //WriteLog("Slot6W: address = %X, byte= %X\n", address & 0x0F, byte);
790 // HandleSlot6(address, byte);
791 uint8_t state = address & 0x0F;
803 floppyDrive.ControlStepper(state);
807 floppyDrive.ControlMotor(state & 0x01);
811 floppyDrive.DriveEnable(state & 0x01);
814 floppyDrive.ReadWrite();
817 floppyDrive.SetLatchValue(byte);
820 floppyDrive.SetReadMode();
823 floppyDrive.SetWriteMode();
829 void HandleSlot6(uint16_t address, uint8_t byte)
834 uint8_t ReadButton0(uint16_t)
836 return (uint8_t)openAppleDown << 7;
840 uint8_t ReadButton1(uint16_t)
842 return (uint8_t)closedAppleDown << 7;
846 // The way the paddles work is that a strobe is written (or read) to $C070,
847 // then software counts down the time that it takes for the paddle outputs
848 // to have bit 7 return to 0. If there are no paddles connected, bit 7
850 // NB: This is really paddles 0-3, not just 0 :-P
851 uint8_t ReadPaddle0(uint16_t)
857 uint8_t ReadIOUDIS(uint16_t)
859 return (uint8_t)ioudis << 7;
863 uint8_t ReadDHIRES(uint16_t)
865 return (uint8_t)dhires << 7;