-
Notifications
You must be signed in to change notification settings - Fork 91
/
amax.cpp
168 lines (146 loc) · 3.26 KB
/
amax.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
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "zfile.h"
#include "amax.h"
#include "custom.h"
#include "memory.h"
#include "newcpu.h"
#include "rommgr.h"
static const int data_scramble[8] = { 3, 2, 4, 5, 7, 6, 0, 1 };
static const int addr_scramble[16] = { 14, 12, 2, 10, 15, 13, 1, 0, 7, 6, 5, 4, 8, 9, 11, 3 };
static int romptr;
static uae_u8 *rom;
static int amax_rom_size, rom_oddeven;
static uae_u8 amax_data;
static uae_u8 bfd100, bfe001;
static uae_u8 dselect;
static bool amax_is_active;
#define AMAX_LOG 0
static void load_byte (void)
{
int addr, i;
uae_u8 val, v;
v = 0xff;
addr = 0;
for (i = 0; i < 16; i++) {
if (romptr & (1 << i))
addr |= 1 << addr_scramble[i];
}
if (rom_oddeven < 0) {
val = v;
} else {
v = rom[addr * 2 + rom_oddeven];
val = 0;
for (i = 0; i < 8; i++) {
if (v & (1 << data_scramble[i]))
val |= 1 << i;
}
}
amax_data = val;
if (AMAX_LOG > 0)
write_log (_T("AMAX: load byte, rom=%d addr=%06x (%06x) data=%02x (%02x) PC=%08X\n"), rom_oddeven, romptr, addr, v, val, M68K_GETPC);
}
static void amax_check (void)
{
/* DIR low = reset address counter */
if ((bfd100 & 2)) {
if (romptr && AMAX_LOG > 0)
write_log (_T("AMAX: counter reset PC=%08X\n"), M68K_GETPC);
romptr = 0;
amax_is_active = false;
}
}
static int dwlastbit;
void amax_diskwrite (uae_u16 w)
{
int i;
/* this is weird, 1->0 transition in disk write line increases address pointer.. */
for (i = 0; i < 16; i++) {
if (dwlastbit && !(w & 0x8000)) {
romptr++;
if (AMAX_LOG > 0)
write_log (_T("AMAX: counter increase %d PC=%08X\n"), romptr, M68K_GETPC);
}
dwlastbit = (w & 0x8000) ? 1 : 0;
w <<= 1;
}
romptr &= amax_rom_size - 1;
amax_check ();
}
static uae_u8 bfe001_ov;
void amax_bfe001_write (uae_u8 pra, uae_u8 dra)
{
uae_u8 v = dra & pra;
bfe001 = v;
/* CHNG low -> high: shift data register */
if ((v & 4) && !(bfe001_ov & 4)) {
amax_data <<= 1;
amax_data |= 1;
if (AMAX_LOG > 0)
write_log (_T("AMAX: data shifted\n"));
}
/* TK0 = even, WPRO = odd */
rom_oddeven = -1;
if ((v & (8 | 16)) != (8 | 16)) {
rom_oddeven = 0;
if (!(v & 16))
rom_oddeven = 1;
}
bfe001_ov = v;
amax_check ();
}
void amax_disk_select (uae_u8 v, uae_u8 ov, int num)
{
bfd100 = v;
dselect = 1 << (num + 3);
if (!(bfd100 & dselect) && (ov & dselect)) {
amax_is_active = true;
load_byte ();
}
amax_check ();
}
uae_u8 amax_disk_status (uae_u8 st)
{
if (!(amax_data & 0x80))
st &= ~0x20;
return st;
}
bool amax_active(void)
{
return amax_is_active;
}
void amax_reset (void)
{
romptr = 0;
rom_oddeven = 0;
bfe001_ov = 0;
dwlastbit = 0;
amax_data = 0xff;
xfree (rom);
rom = NULL;
dselect = 0;
}
void amax_init (void)
{
struct zfile *z = NULL;
if (is_device_rom(&currprefs, ROMTYPE_AMAX, 0) < 0)
return;
amax_reset ();
if (is_device_rom(&currprefs, ROMTYPE_AMAX, 0) > 0)
z = read_device_rom(&currprefs, ROMTYPE_AMAX, 0, NULL);
if (z) {
zfile_fseek (z, 0, SEEK_END);
amax_rom_size = zfile_ftell32(z);
zfile_fseek (z, 0, SEEK_SET);
} else {
write_log (_T("AMAX: failed to load rom\n"));
amax_rom_size = 262144;
}
rom = xcalloc (uae_u8, amax_rom_size);
if (z) {
zfile_fread (rom, amax_rom_size, 1, z);
zfile_fclose (z);
}
write_log (_T("AMAX: loaded, %d bytes\n"), amax_rom_size);
}