-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
338 lines (277 loc) · 9.1 KB
/
main.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
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
#include <xc.h>
#include <stdio.h>
#define _XTAL_FREQ 20000000ul
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define DATA_PORT PORTB
#define DATA_PORT_T TRISB
#define ADDR_PORT PORTD
#define ADDR_PORT_T TRISD
#define RAS PORTCbits.RC0
#define RAS_T TRISCbits.TRISC0
#define CAS PORTCbits.RC1
#define CAS_T TRISCbits.TRISC1
#define WRITE PORTCbits.RC2
#define WRITE_T TRISCbits.TRISC2
// Configured for 8-bit array of 4116 chips.
#define ROW_SIZE 128
#define ROW_COUNT 128
#define ROW_COUNT_MASK 127
typedef unsigned char BYTE;
extern void display_init();
extern void display_logTest(const char* text);
extern void display_logStatus(const char* text);
static char buf[16];
// Cycle after each reset
typedef enum {
// Use the full suite to test DRAM, refresh, whole bits, all patterns, etc...
MODE_FULL_TEST_BY_PAGE,
// Same, but don't use page write
MODE_FULL_TEST_BY_BIT,
// Test a single row for continous write.
// Useful to check power line noise during operations.
MODE_CONTINOUS_WRITE,
// Test write/read a single cell every microsecond. Useful to trace
// timings and response with an oscilloscope. Using a faulty cell it is
// possible to check which type of damage the chip has.
MODE_ONLY_BIT_0,
MODE_FIRST = MODE_FULL_TEST_BY_PAGE,
MODE_LAST = MODE_ONLY_BIT_0
} TEST_PROGRAM_t;
static persistent TEST_PROGRAM_t s_testProgram;
// Write a single bit
static void writeCell(BYTE row, BYTE col, BYTE data) {
// Use early write to avoid contention: DI and DO are shorted
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
DATA_PORT_T = 0;
// Prepare both addr and data
ADDR_PORT = col;
DATA_PORT = data;
NOP();
// Early write
WRITE = 0;
NOP();
// Strobe cas and sample data
CAS = 0;
NOP();
// Deassert both lines
WRITE = 1;
CAS = 1;
// End write
DATA_PORT_T = 0xff;
RAS = 1;
}
// Read a single bit
static BYTE readCell(BYTE row, BYTE col) {
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
// Prepare both addr and data
ADDR_PORT = col;
NOP();
// Strobe cas and sample data
CAS = 0;
NOP();
BYTE b = DATA_PORT;
// Deassert both lines
CAS = 1;
NOP();
RAS = 1;
return b;
}
// Should require less than 2ms (max refresh perdiod): max 70 instructions per cell
static void writeRow_page(BYTE row, BYTE startData, BYTE deltaData) {
BYTE data = startData;
// Page mode write cycle with early write
// Use early write to avoid contention: DI and DO are shorted
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
// Write mode for the whole row
DATA_PORT_T = 0;
for (BYTE col = 0; col < ROW_SIZE; col++) {
// Prepare both addr and data
ADDR_PORT = col;
DATA_PORT = data;
NOP();
// Early write
WRITE = 0;
NOP();
// Strobe cas and sample data
CAS = 0;
NOP();
// Deassert both lines
CAS = 1;
WRITE = 1;
data += deltaData;
}
// End write
RAS = 1;
DATA_PORT_T = 0xff;
}
// Should require less than 2ms (max refresh perdiod): max 70 instructions per cell
static void writeRow_bit(BYTE row, BYTE startData, BYTE deltaData) {
BYTE data = startData;
// Write mode for the whole row
DATA_PORT_T = 0;
for (BYTE col = 0; col < ROW_SIZE; col++) {
// Use early write to avoid contention: DI and DO are shorted
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
// Prepare both addr and data
ADDR_PORT = col;
DATA_PORT = data;
NOP();
// Early write
WRITE = 0;
NOP();
// Strobe cas and sample data
CAS = 0;
NOP();
// Deassert both lines
CAS = 1;
WRITE = 1;
data += deltaData;
RAS = 1;
}
// End write
DATA_PORT_T = 0xff;
}
static void refreshAll(BYTE row);
static void refreshAndWait(BYTE row, short count);
// Should require less than 2ms (max refresh perdiod): max 70 instructions per cell
static void testRow_page(BYTE row, BYTE startData, BYTE deltaData) {
// Page mode read cycle
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
BYTE data = startData;
for (BYTE col = 0; col < ROW_SIZE; col++) {
ADDR_PORT = col;
NOP(); // Stabilize ADDR (long wires)
CAS = 0;
NOP();
BYTE d = DATA_PORT;
CAS = 1;
if ((d & ~0x2) != (data & ~0x2)) {
// Stop row operation
RAS = 1;
// Report error and pause to let the error to be visible
refreshAll(row + 1);
sprintf(buf, "!R%2x C%2x ~%2x", row, col, d);
refreshAll(row + 1);
display_logStatus(buf);
// Wait 0.75 second
refreshAndWait(row + 1, 750);
// Continue the test
ADDR_PORT = row;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
}
data += deltaData;
}
RAS = 1;
}
static void refreshAll(BYTE row) {
for (BYTE i = 0; i < ROW_COUNT; i++, row++) {
// Do refresh
ADDR_PORT = row & ROW_COUNT_MASK;
NOP(); // Stabilize ADDR (long wires)
RAS = 0;
NOP();
NOP();
NOP();
RAS = 1;
}
}
static void refreshAndWait(BYTE row, short count) {
for (short i = 0; i < count; i++) {
refreshAll(row);
__delay_ms(1);
}
}
static void testAllWithRefresh(BYTE startData, BYTE deltaData) {
for (BYTE row = 0x38; row < ROW_COUNT; row++) {
//sprintf(buf, "..W %x", row);
//display_logStatus(buf);
if (s_testProgram == MODE_FULL_TEST_BY_PAGE) {
writeRow_page(row, startData, deltaData);
} else {
writeRow_bit(row, startData, deltaData);
}
// Refresh whole rows starting from the next one
refreshAll(row + 1);
}
// Test memory persistence
// Wait for 512 full refresh cycles (512ms)
refreshAndWait(0, 512);
for (BYTE row = 0x38; row < ROW_COUNT; row++) {
//sprintf(buf, "..R %x", row);
//display_logStatus(buf);
testRow_page(row, startData, deltaData);
// Refresh whole rows starting from the next one
refreshAll(row + 1);
}
}
static void display_testName(const char* name) {
sprintf(buf, "%c %s", s_testProgram == MODE_FULL_TEST_BY_PAGE ? 'P' : 'B', name);
display_logTest(buf);
}
void main(void) {
ADCON1 = 6; // Disable all PORTA and PORTE analog port
WRITE_T = RAS_T = CAS_T = 0;
CAS = RAS = WRITE = 1;
DATA_PORT_T = 0xff; // High-Z
ADDR_PORT_T = 0x00; // Out
OPTION_REGbits.nRBPU = 0; // Pullup to read 1 in case of no RAM
display_init();
// Recommended by Mostek at startup
refreshAndWait(0, 8);
// Not initialized after reset
while (1) {
// Get and increment for next reset
switch (++s_testProgram) {
case MODE_FULL_TEST_BY_PAGE:
case MODE_FULL_TEST_BY_BIT:
// Test all 0's
display_testName("All 0's");
testAllWithRefresh(0, 0);
// Test all 1's
display_testName("All 1's");
testAllWithRefresh(0xff, 0);
// Test alternate bit pattern 1
display_testName("0x55 pattern");
testAllWithRefresh(0x55, 0);
// Test alternate bit pattern 2
display_testName("0xAA pattern");
testAllWithRefresh(0xAA, 0);
// Test incremental pattern
display_testName("+1 pattern ");
testAllWithRefresh(0xaa, 1);
display_logStatus("Test passed!");
while (1) { }
case MODE_CONTINOUS_WRITE:
display_logTest("Continuous write");
while (1) {
writeRow_bit(0, 0, 1);
}
case MODE_ONLY_BIT_0:
display_logTest("Test single bit");
while (1) {
writeCell(0, 0, 0xaa);
__delay_ms(1);
readCell(0, 0);
__delay_ms(1);
}
}
}
}