-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrom.c
193 lines (170 loc) · 5.23 KB
/
brom.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
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
/*
* Copyright (C) 2024 by Matthieu CASTET <[email protected]>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdint.h>
#include <assert.h>
#include "bus.h"
#include "emu.h"
/**
* http://www.datamath.org/Chips/TMC0560.htm
* signal :
* clk/idle : sync input
* ext : input
* IRG : out/HiZ
*
* send 13 bits instruction in S3...S15 LSB fist
*/
struct brom_state {
unsigned int pc;
uint16_t last_ext;
uint16_t last_irg;
/* v1 1k / v2 2.5k */
uint16_t data[1024*5/2];
unsigned int end;
unsigned int start;
};
#define BROM_CS 0
#define PC_ADDR(x) ((x) & 0x3FF)
static unsigned int handle_branch(const struct brom_state *bstate, int cond)
{
int bcond = (bstate->last_irg >> 11) & 1;
int boffset = (bstate->last_irg >> 1) & 0x3FF;
int bneg = (bstate->last_irg) & 1;
//DIS("branch %d %d\n", !!cond, bcond);
if (!!cond == bcond) {
if (bneg)
boffset = -boffset;
return bstate->pc + boffset;
}
return bstate->pc + 1;
}
static void brom_process_out(struct brom_state *bstate, struct bus *bus_state)
{
/* optimisation output state early. S4 */
if (bus_state->sstate != 4)
return;
/* with is implementation, the cpu
* nHOLD(ext) WAIT(irg) XXXX (exec)
* HOLD(ext) WAIT(irg) WAIT (exec)
* HOLD(ext) WAIT(irg) WAIT (exec)
* HOLD(ext) WAIT(irg) WAIT (exec)
* HOLD(ext) WAIT(irg) WAIT (exec)
* nHOLD(ext) KEY(irg) WAIT (exec)
* nHOLD(ext) XXXX(irg) KEY (exec)
*/
if (bus_state->ext & EXT_HOLD) {
/* output same instruction
* HOLD bit has priority over PREG bit
* (see HW manual External debugger)
* */
bstate->pc += 0;
}
/* there is one instruction delay :
* - set KR[1] was set cycle n-2
* - PREG was set in cycle n-1, but we don't have
* new address at the start of this cycle
* and output next instruction
* - use new address at this cycle
*/
else if (bstate->last_ext & EXT_PREG) {
bstate->pc = bstate->last_ext >> 3;
//LOG("PREG 0x%04x\n", bstate->last_ext);
}
/* check branch instruction to update address */
else if (bstate->last_irg & IRG_BRANCH_MASK) {
bstate->pc = handle_branch(bstate, bus_state->ext & EXT_COND);
}
else {
bstate->pc++;
}
//DIS("addr %d last_irg 0x%04x\n", bstate->pc, bstate->last_irg);
/* output instruction if selected. First rom used 1K mask
* but latter can be up to 2.5K.
* Don't use mask for simplicity
*/
if (bstate->pc >= bstate->start && bstate->pc < bstate->end) {
unsigned int addr = bstate->pc - bstate->start;
/* dbg check nobody already write irg
* XXX 0 instruction is valid
*/
assert(bus_state->irg == 0);
bus_state->irg = bstate->data[addr];
bus_state->addr = addr + bstate->start;
//DIS("addr%d new irg%04x\n", addr, bus_state->irg);
}
}
static void brom_process_in(struct brom_state *bstate, struct bus *bus_state)
{
/* optimisation. Read output at last state */
if (bus_state->sstate == 15) {
/* save irg,ext for next cycle */
bstate->last_irg = bus_state->irg;
bstate->last_ext = bus_state->ext;
}
}
static int brom_process(void *priv, struct bus *bus_state)
{
struct brom_state *bstate = priv;
if (bus_state->write)
brom_process_out(bstate, bus_state);
else
brom_process_in(bstate, bus_state);
return 0;
}
static void dis(struct brom_state *bstate)
{
int addr;
int rom_size = bstate->end - bstate->start;
FILE *old_out = log_file;
log_file = stderr;
for (addr = 0; addr < rom_size; addr ++) {
DIS("%04X:\t", addr + bstate->start);
DIS("%04X:\t", bstate->data[addr]);
disasm (addr + bstate->start, bstate->data[addr]);
DIS("\n");
}
log_file = old_out;
}
int brom_init(struct chip *chip, const char *name, int disasm)
{
struct brom_state *bstate = malloc(sizeof(struct brom_state));
unsigned int size;
int base;
if (!bstate)
return -1;
bstate->last_ext = 0;
bstate->last_irg = 0;
bstate->pc = 1<<16;
size = load_dump(bstate->data, sizeof(bstate->data), name, &base);
printf("rom '%s' base %d size %d\n",
name, base, size);
if (!size || size > sizeof(bstate->data)) {
printf("rom invalid\n");
free(bstate);
return -1;
}
bstate->end = base + size;
bstate->start = base;
if (disasm) {
dis(bstate);
}
chip->priv = bstate;
chip->process = brom_process;
return 0;
}