1 /*
2 * libtilemcore - Graphing calculator emulation library
3 *
4 * Copyright (C) 2001 Solignac Julien
5 * Copyright (C) 2004-2011 Benjamin Moody
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <tilem.h>
28
29 #include "xs.h"
30
31 /* FIXME: what effect, if any, do ports 27 and 28 have in memory
32 mapping mode 1? */
33
xs_z80_wrmem(TilemCalc * calc,dword A,byte v)34 void xs_z80_wrmem(TilemCalc* calc, dword A, byte v)
35 {
36 unsigned long pa;
37 byte page;
38
39 page = calc->mempagemap[A>>14];
40
41 if (A & 0x8000) {
42 if (A > (0xFFFF - 64 * calc->hwregs[PORT27]))
43 page = 0x80;
44 else if (A < (0x8000 + 64 * calc->hwregs[PORT28]))
45 page = 0x81;
46 }
47
48 pa = (A & 0x3FFF) + 0x4000L*page;
49
50 if (pa<0x200000) {
51 calc->z80.clock += calc->hwregs[FLASH_WRITE_DELAY];
52 tilem_flash_write_byte(calc, pa, v);
53 }
54 else if (pa < 0x220000) {
55 calc->z80.clock += calc->hwregs[RAM_WRITE_DELAY];
56 *(calc->mem+pa) = v;
57 }
58 }
59
readbyte(TilemCalc * calc,dword pa)60 static inline byte readbyte(TilemCalc* calc, dword pa)
61 {
62 static const byte protectbytes[6] = {0x00,0x00,0xed,0x56,0xf3,0xd3};
63 int state = calc->hwregs[PROTECTSTATE];
64 byte value;
65
66 if (pa < 0x200000 && (calc->flash.state || calc->flash.busy))
67 value = tilem_flash_read_byte(calc, pa);
68 else
69 value = *(calc->mem + pa);
70
71 if (pa < 0x1F0000 || pa >= 0x200000)
72 calc->hwregs[PROTECTSTATE] = 0;
73 else if (state == 6)
74 calc->hwregs[PROTECTSTATE] = 7;
75 else if (state < 6 && value == protectbytes[state])
76 calc->hwregs[PROTECTSTATE] = state + 1;
77 else
78 calc->hwregs[PROTECTSTATE] = 0;
79
80 return (value);
81 }
82
xs_z80_rdmem(TilemCalc * calc,dword A)83 byte xs_z80_rdmem(TilemCalc* calc, dword A)
84 {
85 byte page;
86 unsigned long pa;
87 byte value;
88
89 page = calc->mempagemap[A>>14];
90
91 if (A & 0x8000) {
92 if (A > (0xFFFF - 64 * calc->hwregs[PORT27]))
93 page = 0x80;
94 else if (A < (0x8000 + 64 * calc->hwregs[PORT28]))
95 page = 0x81;
96 }
97
98 if (TILEM_UNLIKELY(page == 0x7E && !calc->flash.unlock)) {
99 tilem_warning(calc, "Reading from read-protected sector");
100 return (0xff);
101 }
102
103 pa = (A & 0x3FFF) + 0x4000L*page;
104
105 if (pa < 0x200000)
106 calc->z80.clock += calc->hwregs[FLASH_READ_DELAY];
107 else
108 calc->z80.clock += calc->hwregs[RAM_READ_DELAY];
109
110 value = readbyte(calc, pa);
111 return (value);
112 }
113
xs_z80_rdmem_m1(TilemCalc * calc,dword A)114 byte xs_z80_rdmem_m1(TilemCalc* calc, dword A)
115 {
116 byte page;
117 unsigned long pa, m;
118 byte value;
119
120 page = calc->mempagemap[A>>14];
121
122 if (A & 0x8000) {
123 if (A > (0xFFFF - 64 * calc->hwregs[PORT27]))
124 page = 0x80;
125 else if (A < (0x8000 + 64 * calc->hwregs[PORT28]))
126 page = 0x81;
127 }
128
129 if (TILEM_UNLIKELY(page == 0x7E && !calc->flash.unlock)) {
130 tilem_warning(calc, "Reading from read-protected sector");
131 return (0xff);
132 }
133
134 pa = (A & 0x3FFF) + 0x4000L*page;
135
136 if (pa < 0x200000) {
137 calc->z80.clock += calc->hwregs[FLASH_EXEC_DELAY];
138
139 if (TILEM_UNLIKELY(page >= calc->hwregs[PORT22]
140 && page <= calc->hwregs[PORT23])) {
141 tilem_warning(calc, "Executing in restricted Flash area");
142 tilem_z80_exception(calc, TILEM_EXC_FLASH_EXEC);
143 }
144 }
145 else {
146 calc->z80.clock += calc->hwregs[RAM_EXEC_DELAY];
147
148 /* Note: this isn't quite strict enough; when port 21
149 is set to 30h, the "ghost" RAM pages 88-8F are
150 treated as distinct pages for restriction purposes.
151 This detail probably isn't worth emulating. */
152 m = pa & calc->hwregs[NO_EXEC_RAM_MASK];
153 if (TILEM_UNLIKELY(m < calc->hwregs[NO_EXEC_RAM_LOWER]
154 || m > calc->hwregs[NO_EXEC_RAM_UPPER])) {
155 tilem_warning(calc, "Executing in restricted RAM area");
156 tilem_z80_exception(calc, TILEM_EXC_RAM_EXEC);
157 }
158 }
159
160 value = readbyte(calc, pa);
161
162 if (TILEM_UNLIKELY(value == 0xff && A == 0x0038)) {
163 tilem_warning(calc, "No OS installed");
164 tilem_z80_exception(calc, TILEM_EXC_FLASH_EXEC);
165 }
166
167 return (value);
168 }
169
xs_mem_ltop(TilemCalc * calc,dword A)170 dword xs_mem_ltop(TilemCalc* calc, dword A)
171 {
172 byte page = calc->mempagemap[A >> 14];
173
174 if (A & 0x8000) {
175 if (A > (0xFFFF - 64 * calc->hwregs[PORT27]))
176 page = 0x80;
177 else if (A < (0x8000 + 64 * calc->hwregs[PORT28]))
178 page = 0x81;
179 }
180
181 return ((page << 14) | (A & 0x3fff));
182 }
183
xs_mem_ptol(TilemCalc * calc,dword A)184 dword xs_mem_ptol(TilemCalc* calc, dword A)
185 {
186 byte page = A >> 14;
187
188 if (!page)
189 return (A & 0x3fff);
190
191 if (page == calc->mempagemap[1])
192 return (0x4000 | (A & 0x3fff));
193
194 if ((A & 0x3fff) < 64 * calc->hwregs[PORT28]) {
195 if (page == 0x81)
196 return (0x8000 | (A & 0x3fff));
197 }
198 else {
199 if (page == calc->mempagemap[2])
200 return (0x8000 | (A & 0x3fff));
201 }
202
203 if ((A & 0x3fff) >= (0x4000 - 64 * calc->hwregs[PORT27])) {
204 if (page == 0x80)
205 return (0xC000 | (A & 0x3fff));
206 }
207 else {
208 if (page == calc->mempagemap[3])
209 return (0xC000 | (A & 0x3fff));
210 }
211
212 return (0xffffffff);
213 }
214