-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPUBUS.cpp
377 lines (289 loc) · 10.6 KB
/
CPUBUS.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
@file
@see cpubus.h for details
@version 1.0
@author Arco van Geest <arco@appeltaart.mine.nu>
@copyright 2013 Arco van Geest <arco@appeltaart.mine.nu> All right reserved.
This file is part of PinDA.
PinDA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PinDA 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with PinDA. If not, see <http://www.gnu.org/licenses/>.
@date 20130520 Initial documented version
@brief interface to the 8bit 68xx memory bus
*/
/******************************************************************************
* Includes
******************************************************************************/
#include "pinda.h"
#include <inttypes.h>
#include <stdio.h>
#include <SPI.h>
#include "mcp23s17.h"
#ifdef ARDUINO
#include "Arduino.h"
#define println(line) Serial.println(line)
#define print(line) Serial.print(line)
#else
#include "arduino_compat.h"
#endif
#include "CPUBUS.h"
/******************************************************************************
* Definitions
******************************************************************************/
/******************************************************************************
* Base class
******************************************************************************/
void CPUBUSClass::interrupt(void ){};
void CPUBUSClass::serviceLoop(void ){};
CPUBUSClass::CPUBUSClass(void){};
// void CPUBUSClass::init(void);
//uint8_t CPUBUSClass::read(int address);
//void CPUBUSClass::write(int address, uint8_t data);
// void CPUBUSClass::e_low();
// void CPUBUSClass::e_high();
// void CPUBUSClass::set_read();
// void CPUBUSClass::set_write();
void CPUBUSClass::reset(void ){};
uint8_t CPUBUSClass::read(const unsigned int address){};
void CPUBUSClass::write(const unsigned int address, const uint8_t data){};
String CPUBUSClass::status(void) {
String status="###CPUBUS STATUS###";
status += "\nSystem type : " + system;
status += "\nName : " + objName;
status += "\nCPUBUS reads : " + String( reads );
status += "\nCPUBUS writes : " + String( writes );
status += "\n";
return status;
} //getStatus
/******************************************************************************
* CPUBUS_Direct
******************************************************************************/
Cpubus_Direct::Cpubus_Direct(String _name) {
writes=0;
reads=0;
objName=_name;
system="Cpubus direct";
//println("CPUBUS init DIRECT_ADDR_DIRECT");
//println("CPUBUS init DIRECT_DATA_CTRL");
CPUBUS_ADDRL_DDR =0xff;
CPUBUS_ADDRH_DDR =0xff;
CPUBUS_DATA_DDR =0x0; //data IO
CPUBUS_DATA_PORT=0xff; //pullup
CPUBUS_CTL_DDR =0x0; // control IO
CPUBUS_CTL_PORT=0xff; //pullup
CPUBUS_ADDRL_PORT = 0x00;
CPUBUS_ADDRH_PORT = 0x00;
pinMode( CPUBUS_E_PIN ,OUTPUT );
pinMode( CPUBUS_VMA_PIN ,OUTPUT );
pinMode( CPUBUS_RW_PIN ,OUTPUT );
// Cpubus_Direct::reset();
} //Cpubus_Direct::Cpubus_Direct
void Cpubus_Direct::reset(void) {
pinMode( CPUBUS_RESET_PIN ,OUTPUT );
digitalWrite(CPUBUS_RESET_PIN, LOW); // Pull reset low
delay(500);
// digitalWrite(CPUBUS_RESET_PIN, HIGH); // no pullup needed
pinMode( CPUBUS_RESET_PIN ,INPUT );
} //reset
uint8_t Cpubus_Direct::read(const unsigned int address) {
reads++; //debug read count
CPUBUS_CTL_PORT &= ~( 1<< CPUBUS_E); //e_low
CPUBUS_CTL_PORT |= ( 1<<CPUBUS_RW); //READ
CPUBUS_ADDRL_PORT = address&0xFF; //Address Low
CPUBUS_ADDRH_PORT = ( address >> 8 )&0xFF ; //Address High
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_VMA); //VMA high
__asm__("nop\n\t");
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_E); //E high
// delay(1);
// for (int del=0;del<100;del++) {
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
// } // (62.5ns on a 16MHz Arduino)
uint8_t data= CPUBUS_DATA_PINS; //get data from bus
CPUBUS_CTL_PORT &= ~( ( 1<< CPUBUS_E ) | (1 << CPUBUS_VMA) ); //E and VMA low
return data;
} //read
void Cpubus_Direct::write(const unsigned int address, const uint8_t value) {
writes++; //debug write count
CPUBUS_CTL_PORT &= ~( 1<< CPUBUS_E); //e_low
CPUBUS_CTL_PORT &= ~( 1<< CPUBUS_RW); //Write
CPUBUS_ADDRL_PORT = address&0xFF; //Address Low
CPUBUS_ADDRH_PORT = (address>>8)&0xFF; //Address High
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_VMA); //VMA high
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
CPUBUS_DATA_DDR =0xff;
// CPUBUS_DATA_PORT= value & 0xff;
CPUBUS_DATA_PORT= value;
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_E)|( 1<< CPUBUS_VMA); //E high
// delay(1);
// for (int del=0;del<10;del++) {
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
// }
CPUBUS_CTL_PORT &= ~( ( 1<< CPUBUS_E ) | (1 << CPUBUS_VMA) ); //E and VMA low
// __asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
CPUBUS_DATA_DDR =0x00; //D0-7 as input
CPUBUS_DATA_PORT=0xff; //Pullup
CPUBUS_CTL_PORT |= ( 1<<CPUBUS_RW); //READ
} //write
void Cpubus_Direct::writeref(const unsigned int & address, const uint8_t & value) {
writes++; //debug write count
CPUBUS_CTL_PORT &= ~( 1<< CPUBUS_E); //e_low
CPUBUS_CTL_PORT &= ~( 1<< CPUBUS_RW); //Write
CPUBUS_ADDRL_PORT = address & 0xFF; //Address Low
CPUBUS_ADDRH_PORT = (address>>8)&0xFF; //Address High
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_VMA); //VMA high
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
CPUBUS_DATA_DDR =0xff;
CPUBUS_DATA_PORT= value;
CPUBUS_CTL_PORT |= ( 1<< CPUBUS_E)|( 1<< CPUBUS_VMA); //E high
// delay(1);
// for (int del=0;del<10;del++) {
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
// }
CPUBUS_CTL_PORT &= ~( ( 1<< CPUBUS_E ) | (1 << CPUBUS_VMA) ); //E and VMA low
// __asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
CPUBUS_DATA_DDR =0x00; //D0-7 as input
CPUBUS_DATA_PORT=0xff; //Pullup
CPUBUS_CTL_PORT |= ( 1<<CPUBUS_RW); //READ
} //write
/******************************************************************************
* CPUBUS_SPI
******************************************************************************/
Cpubus_SPI::Cpubus_SPI(
MCP23S17 * _mcpaddr , MCP23S17 * _mcpdata , String _name
)
{
writes=0;
reads=0;
objName=_name;
system="Cpubus spi";
sv_cpureg=0;
mcpdata=_mcpdata;
mcpaddr=_mcpaddr;
mcpdata->pinMode(INPUT);
mcpaddr->pinMode(OUTPUT);
mcpdata->port( 0xffff); //pullup
mcpaddr->port( 0x0000); //addr 0
mcpdata->pinModeB( CPUBUS_E ,OUTPUT );
mcpdata->pinModeB( CPUBUS_VMA ,OUTPUT );
mcpdata->pinModeB( CPUBUS_RW ,OUTPUT );
} //init
uint8_t Cpubus_SPI::read(unsigned int address) {
//return 0;
reads++; //debug read count
sv_cpureg &= ~( 1<< CPUBUS_E);
sv_cpureg |= ( 1<< CPUBUS_RW);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_E, false ); //E_LOW
// mcpdata->pinB( CPUBUS_RW, true ); //READ
mcpaddr->port( address ); //set address
sv_cpureg |= ( 1<< CPUBUS_VMA);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_VMA, true ); //VMA_HIGH
// __asm__("nop\n\t");
sv_cpureg |= ( 1<< CPUBUS_E);
mcpdata->portB(sv_cpureg);
//mcpdata->pinB( CPUBUS_E, true ); //E_HIGH
uint8_t data= mcpdata->portA(); //get data from bus
sv_cpureg &= ~(( 1<< CPUBUS_E)|(1<<CPUBUS_VMA));
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_E, false ); //E_LOW
// mcpdata->pinB( CPUBUS_VMA, false ); //VMA_LOW
return data;
} //read
void Cpubus_SPI::write(unsigned int address, uint8_t value) {
//return;
writes++; //debug write count
sv_cpureg &= ~(( 1<< CPUBUS_E)|(1<<CPUBUS_RW));
mcpdata->portB(sv_cpureg);
//mcpdata->pinB( CPUBUS_E, false ); //E_LOW
//mcpdata->pinB( CPUBUS_RW, false ); //WRITE
mcpaddr->port( address ); //set address
sv_cpureg |= ( 1<< CPUBUS_VMA);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_VMA, true ); //VMA_HIGH
// __asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
mcpdata->ddrA(0xFF);
mcpdata->portA(value);
sv_cpureg |= ( 1<< CPUBUS_E);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_E, true ); //E_HIGH
// __asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
// mcpdata->pinB( CPUBUS_E, false ); //E_LOW
sv_cpureg &= ~(( 1<< CPUBUS_E));
mcpdata->portB(sv_cpureg);
mcpdata->ddrA(0x00);
// mcpdata->pinB( CPUBUS_RW, true ); //READ
// mcpdata->pinB( CPUBUS_VMA, false ); //VMA_LOW
sv_cpureg |= ( 1<< CPUBUS_RW);
sv_cpureg &= ~(( 1<< CPUBUS_E)|(1<<CPUBUS_VMA));
mcpdata->portB(sv_cpureg);
} //write
/*
OLD
uint8_t Cpubus_SPI::read(unsigned int address) {
//return 0;
reads++; //debug read count
sv_cpureg &= ~( 1<< CPUBUS_E);
sv_cpureg |= ( 1<< CPUBUS_RW);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_E, false ); //E_LOW
// mcpdata->pinB( CPUBUS_RW, true ); //READ
mcpaddr->port( address ); //set address
sv_cpureg |= ( 1<< CPUBUS_VMA);
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_VMA, true ); //VMA_HIGH
// __asm__("nop\n\t");
sv_cpureg |= ( 1<< CPUBUS_E);
mcpdata->portB(sv_cpureg);
//mcpdata->pinB( CPUBUS_E, true ); //E_HIGH
uint8_t data= mcpdata->portA(); //get data from bus
sv_cpureg &= ~(( 1<< CPUBUS_E)|(1<<CPUBUS_VMA));
mcpdata->portB(sv_cpureg);
// mcpdata->pinB( CPUBUS_E, false ); //E_LOW
// mcpdata->pinB( CPUBUS_VMA, false ); //VMA_LOW
return data;
} //read
void Cpubus_SPI::write(unsigned int address, uint8_t value) {
//return;
writes++; //debug write count
mcpdata->pinB( CPUBUS_E, false ); //E_LOW
mcpdata->pinB( CPUBUS_RW, false ); //WRITE
mcpaddr->port( address ); //set address
mcpdata->pinB( CPUBUS_VMA, true ); //VMA_HIGH
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
mcpdata->ddrA(0xFF);
mcpdata->portA(value);
mcpdata->pinB( CPUBUS_E, true ); //E_HIGH
__asm__("nop\n\t"); // (62.5ns on a 16MHz Arduino)
mcpdata->pinB( CPUBUS_E, false ); //E_LOW
mcpdata->ddrA(0x00);
mcpdata->pinB( CPUBUS_RW, true ); //READ
mcpdata->pinB( CPUBUS_VMA, false ); //VMA_LOW
} //write
*/
/*
CPUBUSClass::CPUBUSClass( void) {
println("CPUBUS");
}
*/
/*
void Cpubus_SPI::init(void ){
println("CPUBUS init SPI_ADDR");
println("CPUBUS init SPI_DATA_CTRL");
}
*/
/*
int main( void){
CPUBUSClass Cpubus;
Cpubus.init( );
Cpubus.read(0x20);
Cpubus.write(0x3000,128);
}
*/