| 1 |
/* |
| 2 |
* KCemu -- The emulator for the KC85 homecomputer series and much more. |
| 3 |
* Copyright (C) 1997-2010 Torsten Paul |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <fstream> |
| 22 |
|
| 23 |
#include "kc/system.h" |
| 24 |
#include "kc/prefs/types.h" |
| 25 |
|
| 26 |
#include "kc/kc.h" |
| 27 |
#include "kc/z80.h" |
| 28 |
#include "kc/muglerpc/memory.h" |
| 29 |
|
| 30 |
using namespace std; |
| 31 |
|
| 32 |
MemoryMuglerPC::MemoryMuglerPC(void) : Memory() |
| 33 |
{ |
| 34 |
load_rom(SystemROM::ROM_KEY_SYSTEM, &_rom); |
| 35 |
load_rom(SystemROM::ROM_KEY_CHARGEN, &_rom_chargen); |
| 36 |
|
| 37 |
memory_group_t mem[] = { |
| 38 |
{ &_m_scr, "-", 0x0000, 0x10000, 0, 256, 0, 1 }, |
| 39 |
{ &_m_rom, "ROM", 0x0000, 0x2000, &_rom[0], 10, 1, 1 }, |
| 40 |
{ &_m_ram0_lo, "RAM Bank 0 (lo)", 0x0000, 0xc000, &_ram_block0[0], 20, 0, 1 }, |
| 41 |
{ &_m_ram0_hi, "RAM Bank 0 (hi)", 0xc000, 0x4000, &_ram_block0[0xc000], 20, 0, 1 }, |
| 42 |
{ &_m_ram1, "RAM Bank 1", 0x0000, 0x10000, &_ram_block1[0], 30, 0, 0 }, |
| 43 |
{ &_m_ram2, "RAM Bank 2", 0x0000, 0x10000, &_ram_block2[0], 30, 0, 0 }, |
| 44 |
/* |
| 45 |
* The irm is not set to active because it is currently not used via the normal memory |
| 46 |
* switch. Instead it's always written to in memWrite8(). All writes to to both the active |
| 47 |
* ram block and the irm. Read access to irm is not possible and always goes to the active |
| 48 |
* ram block. |
| 49 |
*/ |
| 50 |
{ &_m_irm, "IRM", 0xf800, 0x0800, &_irm[0], 10, 0, 0 }, |
| 51 |
{ 0, }, |
| 52 |
}; |
| 53 |
init_memory_groups(mem); |
| 54 |
|
| 55 |
_m_irm->set_read_through(true); |
| 56 |
|
| 57 |
reset(true); |
| 58 |
z80->register_ic(this); |
| 59 |
} |
| 60 |
|
| 61 |
MemoryMuglerPC::~MemoryMuglerPC(void) |
| 62 |
{ |
| 63 |
z80->unregister_ic(this); |
| 64 |
|
| 65 |
delete _m_scr; |
| 66 |
delete _m_rom; |
| 67 |
delete _m_ram0_lo; |
| 68 |
delete _m_ram0_hi; |
| 69 |
delete _m_ram1; |
| 70 |
delete _m_ram2; |
| 71 |
delete _m_irm; |
| 72 |
} |
| 73 |
|
| 74 |
byte_t |
| 75 |
MemoryMuglerPC::memRead8(word_t addr) |
| 76 |
{ |
| 77 |
return _memrptr[addr >> MemArea::PAGE_SHIFT][addr & MemArea::PAGE_MASK]; |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
MemoryMuglerPC::memWrite8(word_t addr, byte_t val) |
| 82 |
{ |
| 83 |
_memwptr[addr >> MemArea::PAGE_SHIFT][addr & MemArea::PAGE_MASK] = val; |
| 84 |
if (addr >= 0xf800) |
| 85 |
_irm[addr - 0xf800] = val; |
| 86 |
} |
| 87 |
|
| 88 |
byte_t * |
| 89 |
MemoryMuglerPC::get_irm(void) |
| 90 |
{ |
| 91 |
return _irm; |
| 92 |
} |
| 93 |
|
| 94 |
byte_t * |
| 95 |
MemoryMuglerPC::get_char_rom(void) |
| 96 |
{ |
| 97 |
return _rom_chargen; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
MemoryMuglerPC::reset(bool power_on) |
| 102 |
{ |
| 103 |
_m_rom->set_active(true); |
| 104 |
_m_ram0_lo->set_active(true); |
| 105 |
_m_ram0_hi->set_active(true); |
| 106 |
_m_ram1->set_active(false); |
| 107 |
_m_ram2->set_active(false); |
| 108 |
_m_irm->set_active(false); // see above |
| 109 |
reload_mem_ptr(); |
| 110 |
|
| 111 |
if (!power_on) |
| 112 |
return; |
| 113 |
|
| 114 |
scratch_mem(&_irm[0], 0x800); |
| 115 |
scratch_mem(&_ram_block0[0], 0x10000); |
| 116 |
scratch_mem(&_ram_block1[0], 0x10000); |
| 117 |
scratch_mem(&_ram_block2[0], 0x10000); |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
MemoryMuglerPC::select_eprom(bool active) |
| 122 |
{ |
| 123 |
_m_rom->set_active(active); |
| 124 |
reload_mem_ptr(); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
MemoryMuglerPC::select_ram_block(int block, bool force_ram0_at_c000h) |
| 129 |
{ |
| 130 |
_m_ram0_lo->set_active(block == 0); |
| 131 |
_m_ram0_hi->set_active((block == 0) || force_ram0_at_c000h); |
| 132 |
_m_ram1->set_active(block == 1); |
| 133 |
_m_ram2->set_active(block == 2); |
| 134 |
reload_mem_ptr(); |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
MemoryMuglerPC::dumpCore(void) |
| 139 |
{ |
| 140 |
ofstream os; |
| 141 |
|
| 142 |
os.open("core.z80"); |
| 143 |
|
| 144 |
cerr << "Memory: dumping core..." << endl; |
| 145 |
if (!os) |
| 146 |
{ |
| 147 |
cerr << "Memory: can't write 'core.z80'" << endl; |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
for (int a = 0;a < 0x10000;a++) |
| 152 |
os.put(memRead8(a)); |
| 153 |
|
| 154 |
os.close(); |
| 155 |
cerr << "Memory: done." << endl; |
| 156 |
} |