-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathram.c
124 lines (102 loc) · 2.34 KB
/
ram.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "mmu.h"
#include "state.h"
#include "diag.h"
#define RAMSIZE (4*1048576)-8
#define RAMBASE 8
#define RAMCFGSIZE 1
#define RAMCFGBASE 0xff8001
#define RAM_PHYSMAX 0x100000-1
static BYTE ramcfg;
static BYTE *memory;
HANDLE_DIAGNOSTICS(ram)
static BYTE *real(LONG addr)
{
return memory+addr-RAMBASE;
}
static BYTE ram_read_byte(LONG addr)
{
if(addr > RAM_PHYSMAX) return 0;
MMU_WAIT_STATES();
return *(real(addr));
}
WORD ram_read_word(LONG addr)
{
return (ram_read_byte(addr)<<8)|ram_read_byte(addr+1);
}
WORD ram_read_word_shifter(LONG addr)
{
if(addr > RAM_PHYSMAX) return 0;
return (*real(addr) << 8) + *real(addr+1);
}
static void ram_write_byte(LONG addr, BYTE data)
{
MMU_WAIT_STATES();
*(real(addr)) = data;
}
static void ram_write_word(LONG addr, WORD data)
{
ram_write_byte(addr, (data&0xff00)>>8);
ram_write_byte(addr+1, (data&0xff));
}
static int ram_state_collect(struct mmu_state *state)
{
state->size = RAM_PHYSMAX+1-RAMBASE;
state->data = xmalloc(state->size);
if(state->data == NULL)
return STATE_INVALID;
memcpy(state->data, memory, state->size);
return STATE_VALID;
}
static void ram_state_restore(struct mmu_state *state)
{
long size;
size = state->size;
if(state->size > (RAM_PHYSMAX+1-RAMBASE))
size = RAM_PHYSMAX+1-RAMBASE;
memcpy(memory, state->data, size);
}
void ram_clear(void)
{
memset( memory, 0, sizeof(BYTE) * RAMSIZE );
}
static BYTE ramcfg_read_byte(LONG addr)
{
return ramcfg;
}
static void ramcfg_write_byte(LONG addr, BYTE data)
{
ramcfg = data;
}
static void cfg_diagnostics()
{
}
void ram_init()
{
struct mmu *ram,*cfg;
memory = xmalloc(sizeof(BYTE) * RAMSIZE);
if(!memory) {
return;
}
ram = mmu_create("RAM0", "RAM");
ram->start = RAMBASE;
ram->size = RAMSIZE;
ram->read_byte = ram_read_byte;
ram->read_word = ram_read_word;
ram->write_byte = ram_write_byte;
ram->write_word = ram_write_word;
ram->state_collect = ram_state_collect;
ram->state_restore = ram_state_restore;
ram->diagnostics = ram_diagnostics;
mmu_register(ram);
cfg = mmu_create("CFG0", "RAM Configuration");
cfg->start = RAMCFGBASE;
cfg->size = RAMCFGSIZE;
cfg->read_byte = ramcfg_read_byte;
cfg->write_byte = ramcfg_write_byte;
cfg->diagnostics = cfg_diagnostics;
mmu_register(cfg);
}