forked from pdaxrom/pyldin2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdramsim.h
108 lines (93 loc) · 2.76 KB
/
sdramsim.h
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
#ifndef SDRAMSIM_H
#define NBANKS 4
#define POWERED_UP_STATE 6
#define CLK_RATE_HZ 1000000000 // = 100 MHz = 100 * 10^6
#define PWRUP_WAIT_CKS ((int)(.000200 * CLK_RATE_HZ))
#define MAX_BANKOPEN_TIME ((int)(.000100 * CLK_RATE_HZ))
#define MAX_REFRESH_TIME ((int)(.064 * CLK_RATE_HZ))
#define SDRAM_QSZ 16
#define LGSDRAMSZB 24
#define SDRAMSZB (1<<LGSDRAMSZB)
class SDRAMSIM {
int m_pwrup;
short *m_mem;
short m_last_value, m_qmem[4];
int m_bank_status[NBANKS];
int m_bank_row[NBANKS];
int m_bank_open_time[NBANKS];
unsigned *m_refresh_time;
int m_refresh_loc, m_nrefresh;
int m_qloc, m_qdata[SDRAM_QSZ], m_qmask, m_wr_addr;
int m_clocks_till_idle;
bool m_next_wr;
unsigned m_fail;
public:
SDRAMSIM(void) {
m_mem = new short[SDRAMSZB/2]; // 32 MB, or 16 Mshorts
m_nrefresh = 1<<13;
m_refresh_time = new unsigned[m_nrefresh];
for(int i=0; i<m_nrefresh; i++)
m_refresh_time[i] = 0;
m_refresh_loc = 0;
m_pwrup = 0;
m_clocks_till_idle = 0;
m_last_value = 0;
m_clocks_till_idle = PWRUP_WAIT_CKS;
fprintf(stderr, "POWER UP WAIT :%d\n", PWRUP_WAIT_CKS);
m_wr_addr = 0;
m_qloc = 0;
m_qmask = SDRAM_QSZ-1;
m_next_wr = true;
m_fail = 0;
}
~SDRAMSIM(void) {
delete m_mem;
}
short operator()(int clk, int cke,
int cs_n, int ras_n, int cas_n, int we_n, int bs,
unsigned addr,
int driv, short data, short dqm);
int pwrup(void) const { return m_pwrup; }
void load(unsigned addr, const char *data, size_t len) {
short *dp;
const char *sp = data;
unsigned base;
assert((addr&1)==0);
base = addr & (SDRAMSZB-1);
assert((len&1)==0);
assert(addr + len < SDRAMSZB);
//dp = &m_mem[(base>>1)];
dp = &m_mem[base];
fprintf(stderr, "base %x\n", (base>>1));
for(unsigned k=0; k<len/2; k++) {
short v;
v = (sp[0]<<8)|(sp[0]&0x0ff);
*dp = v;
//fprintf(stderr, "first byte 0x%04hX \n", *dp);
dp++;
v = (sp[1]<<8)|(sp[1]&0x0ff);
*dp = v;
//fprintf(stderr, "second byte 0x%04hX \n", *dp);
dp++;
sp+=2;
}
}
/*void load(unsigned addr, const char *data, size_t len) {
short *dp;
const char *sp = data;
unsigned base;
assert((addr&1)==0);
base = addr & (SDRAMSZB-1);
assert((len&1)==0);
assert(addr + len < SDRAMSZB);
dp = &m_mem[(base>>1)];
printf("base %x %x\n", base, (base>>1));
for(unsigned k=0; k<len/2; k++) {
short v;
v = (sp[0]<<8)|(sp[1]&0x0ff);
sp+=2;
*dp++ = v;
}
}*/
};
#endif