-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMemory.cpp
702 lines (642 loc) · 19.7 KB
/
Memory.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
/*
*--------------------------------------------------------------------
* Project: VM65 - Virtual Machine/CPU emulator programming
* framework.
*
* File: Memory.cpp.
*
* Purpose: Implementation of class Memory.
* The Memory class implements the highest abstraction
* layer of Visrtual Machine's memory. in this particular
* case the Virtual Machine emulates a computer system
* based on a 8-bit microprocessor. Therefore it
* implements image size and addressing space adequate
* for the specific architecture of such microprocessor.
* The Memory class also interfaces with the MemMapDev
* API (Memory Mapped Devices).
*
* Date:
*
* Copyright: (C) by Marek Karcz 2016. All rights reserved.
*
* Contact: makarcz@yahoo.com
*
* License Agreement and Warranty:
This software is provided with No Warranty.
I (Marek Karcz) will not be held responsible for any damage to
computer systems, data or user's health resulting from use.
Please proceed responsibly and apply common sense.
This software is provided in hope that it will be useful.
It is free of charge for non-commercial and educational use.
Distribution of this software in non-commercial and educational
derivative work is permitted under condition that original
copyright notices and comments are preserved. Some 3-rd party work
included with this project may require separate application for
permission from their respective authors/copyright owners.
*--------------------------------------------------------------------
*/
#include "Memory.h"
#include "MKGenException.h"
//#define DBG 1
#if defined (DBG)
#include <iostream>
using namespace std;
#endif
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
namespace MKBasic {
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
Memory::Memory()
{
Initialize();
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
Memory::~Memory()
{
if (NULL != mpMemMapDev) delete mpMemMapDev;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Memory::Initialize()
{
unsigned short addr = 0;
for (int i=0; i < 0xFFFF; i++) {
m8bitMem[addr++] = 0;
}
for (int i=0; i < MEM_PAGE_SIZE; i++) {
mMemPageDev[i] = -1;
}
mCharIOAddr = CHARIO_ADDR;
mCharIOActive = false;
mIOEcho = false;
mROMBegin = ROM_BEGIN;
mROMEnd = ROM_END;
mROMActive = false;
mpMemMapDev = new MemMapDev(this);
mGraphDispActive = false;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Memory::EnableROM()
{
mROMActive = true;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Memory::DisableROM()
{
mROMActive = false;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Memory::SetROM(unsigned short start, unsigned short end)
{
if (mROMEnd > mROMBegin) {
mROMBegin = start;
mROMEnd = end;
}
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Memory::EnableROM(unsigned short start, unsigned short end)
{
SetROM(start,end);
EnableROM();
}
/*
*--------------------------------------------------------------------
* Method: Peek8bit()
* Purpose: Get/read 8-bit value from memory. If the memory address
* is within the range of any active devices, call
* handling method for this device.
* Arguments: addr - memory address
* Returns: unsigned char value read from specified memory address
*--------------------------------------------------------------------
*/
unsigned char Memory::Peek8bit(unsigned short addr)
{
// if memory address is in range of any active memory mapped
// devices, call corresponding device handling function
int mempg = addr / MEM_PAGE_SIZE;
if (mMemPageDev[mempg] >= 0) {
bool cont_loop = true;
for (vector<Device>::iterator devit = mActiveDeviceVec.begin();
devit != mActiveDeviceVec.end() && cont_loop;
++devit
) {
if (devit->num >= 0) {
for (MemAddrRanges::iterator memrangeit = devit->addr_ranges.begin();
memrangeit != devit->addr_ranges.end();
++memrangeit
) {
if (addr >= memrangeit->start_addr && addr <= memrangeit->end_addr) {
ReadFunPtr pfun = devit->read_fun_ptr;
if (pfun != NULL) {
cont_loop = false;
(mpMemMapDev->*pfun)((int)addr);
break;
}
}
}
mDispOp = (DEVNUM_GRDISP == devit->num);
}
}
}
return m8bitMem[addr];
}
/*
*--------------------------------------------------------------------
* Method: Peek8bitImg()
* Purpose: Get/read 8-bit value from memory image only.
* Memory mapped devices are not affected.
* Arguments: addr - memory address
* Returns: unsigned char value read from specified memory address
*--------------------------------------------------------------------
*/
unsigned char Memory::Peek8bitImg(unsigned short addr)
{
return m8bitMem[addr];
}
/*
*--------------------------------------------------------------------
* Method: Peek16bit()
* Purpose: Get/read 16-bit value from memory. If the memory address
* is within the range of any active devices, call
* handling method for this device.
* Arguments: addr - memory address
* Returns: unsigned short value read secified from memory address
* and next memory address (16-bit, little endian)
*--------------------------------------------------------------------
*/
unsigned short Memory::Peek16bit(unsigned short addr)
{
unsigned short ret = 0;
// if memory address is in range of any active memory mapped
// devices, call corresponding device handling function
int mempg = addr / MEM_PAGE_SIZE;
if (mMemPageDev[mempg] >= 0) {
bool cont_loop = true;
for (vector<Device>::iterator devit = mActiveDeviceVec.begin();
devit != mActiveDeviceVec.end() && cont_loop;
++devit
) {
if (devit->num >= 0) {
for (MemAddrRanges::iterator memrangeit = devit->addr_ranges.begin();
memrangeit != devit->addr_ranges.end();
++memrangeit
) {
if (addr >= memrangeit->start_addr && addr <= memrangeit->end_addr) {
ReadFunPtr pfun = devit->read_fun_ptr;
if (pfun != NULL) {
cont_loop = false;
(mpMemMapDev->*pfun)((int)addr);
break;
}
}
}
mDispOp = (DEVNUM_GRDISP == devit->num);
}
}
}
ret = m8bitMem[addr++];
ret += m8bitMem[addr] * 256;
return ret;
}
/*
*--------------------------------------------------------------------
* Method: Poke8bit()
* Purpose: Write byte to specified memory location.
* If the memory location is mapped to an active device,
* call corresponding handling function.
* If the memory location is protected (ROM), do not
* write the value.
* Arguments: addr - (0x0000..0xffff) memory address,
* val - value (0x00..0xff)
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::Poke8bit(unsigned short addr, unsigned char val)
{
// if memory address is in range of any active memory mapped
// devices, call corresponding device handling function
int mempg = addr / MEM_PAGE_SIZE;
if (mMemPageDev[mempg] >= 0) {
bool cont_loop = true;
for (vector<Device>::iterator devit = mActiveDeviceVec.begin();
devit != mActiveDeviceVec.end() && cont_loop;
++devit
) {
if (devit->num >= 0) {
for (MemAddrRanges::iterator memrangeit = devit->addr_ranges.begin();
memrangeit != devit->addr_ranges.end();
++memrangeit
) {
if (addr >= memrangeit->start_addr && addr <= memrangeit->end_addr) {
WriteFunPtr pfun = devit->write_fun_ptr;
if (pfun != NULL) {
cont_loop = false;
(mpMemMapDev->*pfun)((int)addr,(int)val);
break;
}
}
}
mDispOp = (DEVNUM_GRDISP == devit->num);
}
}
}
if (!mROMActive || (addr < mROMBegin || addr > mROMEnd)) {
m8bitMem[addr] = val;
}
}
/*
*--------------------------------------------------------------------
* Method: Poke8bitImg()
* Purpose: Write byte to specified memory location.
* Arguments: addr - (0x0000..0xffff) memory address,
* val - value (0x00..0xff)
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::Poke8bitImg(unsigned short addr, unsigned char val)
{
m8bitMem[addr] = val;
}
/*
*--------------------------------------------------------------------
* Method: SetCharIO()
* Purpose: Activates and sets an address of basic character I/O
* emulation (console).
* Arguments: addr - address of I/O area (0x0000..0xFFFF)
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::SetCharIO(unsigned short addr, bool echo)
{
mCharIOAddr = addr;
mIOEcho = echo;
AddrRange addr_range(addr, addr+1);
MemAddrRanges memaddr_ranges;
DevPar dev_par("echo",(echo?"true":"false"));
DevParams dev_params;
dev_params.push_back(dev_par);
memaddr_ranges.push_back(addr_range);
SetupDevice(DEVNUM_CHARIO, memaddr_ranges, dev_params);
if (false == mCharIOActive) AddDevice(DEVNUM_CHARIO);
mCharIOActive = true;
mpMemMapDev->ActivateCharIO();
}
/*
*--------------------------------------------------------------------
* Method: DisableCharIO()
* Purpose: Deactivates basic character I/O emulation (console).
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::DisableCharIO()
{
mCharIOActive = false;
DeleteDevice(DEVNUM_CHARIO);
mpMemMapDev->DeactivateCharIO();
}
/*
*--------------------------------------------------------------------
* Method: GetCharIOAddr()
* Purpose: Returns current address of basic character I/O area.
* Arguments: n/a
* Returns: address of I/O area
*--------------------------------------------------------------------
*/
unsigned short Memory::GetCharIOAddr()
{
return mCharIOAddr;
}
/*
*--------------------------------------------------------------------
* Method: SetGraphDisp()
* Purpose: Setup and activate graphics display device.
* Arguments: addr - base address of display device
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::SetGraphDisp(unsigned short addr)
{
AddrRange addr_range(addr, addr + GRAPHDEVREG_END - 1);
MemAddrRanges memaddr_ranges;
DevPar dev_par("nil","nil");
DevParams dev_params;
dev_params.push_back(dev_par);
memaddr_ranges.push_back(addr_range);
SetupDevice(DEVNUM_GRDISP, memaddr_ranges, dev_params);
if (false == mGraphDispActive) AddDevice(DEVNUM_GRDISP);
mGraphDispActive = true;
mpMemMapDev->ActivateGraphDisp();
}
/*
*--------------------------------------------------------------------
* Method: DisableGraphDisp()
* Purpose: Inactivate graphics display device.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::DisableGraphDisp()
{
mGraphDispActive = false;
mpMemMapDev->DeactivateGraphDisp();
DeleteDevice(DEVNUM_GRDISP);
}
/*
*--------------------------------------------------------------------
* Method: GetGraphDispAddr()
* Purpose: Return base address of graphics display device.
* Arguments: n/a
* Returns: unsigned short - address ($0000 - $FFFF)
*--------------------------------------------------------------------
*/
unsigned short Memory::GetGraphDispAddr()
{
return mpMemMapDev->GetGraphDispAddrBase();
}
/*
*--------------------------------------------------------------------
* Method: GetROMBegin()
* Purpose: Get starting address of read-only memory.
* Arguments:
* Returns: unsigned short - address ($0000-$FFFF)
*--------------------------------------------------------------------
*/
unsigned short Memory::GetROMBegin()
{
return mROMBegin;
}
/*
*--------------------------------------------------------------------
* Method: GetROMEnd()
* Purpose: Get end address of read-only memory.
* Arguments:
* Returns: unsigned short - address ($0000-$FFFF)
*--------------------------------------------------------------------
*/
unsigned short Memory::GetROMEnd()
{
return mROMEnd;
}
/*
*--------------------------------------------------------------------
* Method: IsROMEnabled()
* Purpose: Get status of ROM.
* Arguments:
* Returns: bool - true if enabled.
*--------------------------------------------------------------------
*/
bool Memory::IsROMEnabled()
{
return mROMActive;
}
/*
*--------------------------------------------------------------------
* Method: AddDevice()
* Purpose: Add device to active devices cache.
* Arguments: devnum - device number
* Returns: -1 if device is not supported OR already cached
* devnum - device number if it was found
*--------------------------------------------------------------------
*/
int Memory::AddDevice(int devnum)
{
int ret = -1;
bool found = false;
Device dev = mpMemMapDev->GetDevice(devnum);
if (dev.num >= 0) {
for (vector<Device>::iterator devit = mActiveDeviceVec.begin();
devit != mActiveDeviceVec.end();
++devit
) {
if (devit->num == devnum) {
found = true;
break;
}
}
// if device not found in local cache, add it
if (!found) {
mActiveDeviceVec.push_back(dev);
ret = devnum;
// update the device usage flag in memory pages devices array mMemPageDev
for (MemAddrRanges::iterator memrangeit = dev.addr_ranges.begin();
memrangeit != dev.addr_ranges.end();
++memrangeit
) {
int pgnum = memrangeit->start_addr / MEM_PAGE_SIZE;
while (pgnum < MEM_PAGE_SIZE) {
mMemPageDev[pgnum] = devnum;
pgnum++;
if (pgnum * MEM_PAGE_SIZE > memrangeit->end_addr) break;
}
}
}
} // END if (dev.num >= 0)
// else device with such number is not supported
return ret;
}
/*
*--------------------------------------------------------------------
* Method: DeleteDevice()
* Purpose: Delete device from active devices cache.
* Arguments: devnum - device number
* Returns: >=0 if device was found in local cache and deleted
* -1 if device was not found
*--------------------------------------------------------------------
*/
int Memory::DeleteDevice(int devnum)
{
vector<Device> actdev_new;
int ret = -1;
for (int i=0; i < MEM_PAGE_SIZE; i++) {
mMemPageDev[i] = -1;
}
// device is deleted by refreshing local active devices cache
// the device to be deleted is skipped and not re-added to refreshed
// cache vector
for (vector<Device>::iterator devit = mActiveDeviceVec.begin();
devit != mActiveDeviceVec.end();
++devit
) {
if (devit->num != devnum) {
Device dev = mpMemMapDev->GetDevice(devit->num);
if (dev.num < 0)
throw MKGenException("Unsupported device in local cache");
actdev_new.push_back(mpMemMapDev->GetDevice(devit->num));
// update the device number in memory pages devices array mMemPageDev
for (MemAddrRanges::iterator memrangeit = devit->addr_ranges.begin();
memrangeit != devit->addr_ranges.end();
++memrangeit
) {
int pgnum = memrangeit->start_addr / MEM_PAGE_SIZE;
while (pgnum < MEM_PAGE_SIZE) {
mMemPageDev[pgnum] = devit->num;
pgnum++;
if (pgnum * MEM_PAGE_SIZE > memrangeit->end_addr) break;
}
}
} else ret++; // indicating that the device was found in cache
}
// refresh local active devices cache
mActiveDeviceVec.clear();
mActiveDeviceVec = actdev_new;
return ret;
}
/*
*--------------------------------------------------------------------
* Method: SetupDevice()
* Purpose: Configure device address ranges and parameters.
* Arguments: devnum - device number, must be on active dev. list
* memranges - memory address ranges vector
* params - parameters vector
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::SetupDevice(int devnum,
MemAddrRanges memranges,
DevParams params)
{
Device dev = mpMemMapDev->GetDevice(devnum);
if (dev.num >= 0) {
mpMemMapDev->SetupDevice(devnum, memranges, params);
// reload device to local vector because its setup has changed
// but only if device was cached locally already (active)
if (0 <= DeleteDevice(devnum)) AddDevice(devnum);
}
if (DEVNUM_CHARIO == devnum) {
mCharIOAddr = mpMemMapDev->GetCharIOAddr();
mIOEcho = mpMemMapDev->GetCharIOEchoOn();
}
}
/*
*--------------------------------------------------------------------
* Method: GetCharIn()
* Purpose: Return character from the emulated character I/O FIFO
* input buffer or -1 if FIFO empty or char I/O not active.
* Arguments: n/a
* Returns: character
*--------------------------------------------------------------------
*/
char Memory::GetCharIn()
{
return mpMemMapDev->GetCharIn();
}
/*
*--------------------------------------------------------------------
* Method: GetCharOut()
* Purpose: Return character from the emulated character I/O FIFO
* output buffer or -1 if FIFO empty or char I/O not
* active.
* Arguments: n/a
* Returns: character
*--------------------------------------------------------------------
*/
char Memory::GetCharOut()
{
return mpMemMapDev->GetCharOut();
}
/*
*--------------------------------------------------------------------
* Method: GraphDisp_ReadEvents()
* Purpose: Read events from the graphics display window.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::GraphDisp_ReadEvents()
{
mpMemMapDev->GraphDisp_ReadEvents();
}
/*
*--------------------------------------------------------------------
* Method: GraphDisp_Update()
* Purpose: Trigger update handler of the graphics display window.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Memory::GraphDisp_Update()
{
mpMemMapDev->GraphDisp_Update();
}
/*
*--------------------------------------------------------------------
* Method: GraphDispOp()
* Purpose: Status of last operation being perf. on Graphics Display
* or not.
* Arguments: n/a
* Returns: bool, true if last operation was performed on Graphics
* Display device.
*--------------------------------------------------------------------
*/
bool Memory::GraphDispOp()
{
return mDispOp;
}
/*
*--------------------------------------------------------------------
* Method: GetMemMapDevPtr()
* Purpose: Get the pointer to MemMapDev object.
* Arguments: n/a
* Returns: Pointer to MemMapDev object.
*--------------------------------------------------------------------
*/
MemMapDev *Memory::GetMemMapDevPtr()
{
return mpMemMapDev;
}
} // namespace MKBasic