OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sim/] [verilated/] [memsim.cpp] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 58 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    memsim.cpp
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     This creates a memory like device to act on a WISHBONE bus.
8
//              It doesn't exercise the bus thoroughly, but does give some
9
//              exercise to the bus to see whether or not the bus master
10
//              can control it.
11
//
12
//      This particular version differs from the memsim version within the
13
//      ZipCPU project in that there is a variable delay from request to
14
//      completion.
15
//
16
//
17
// Creator:     Dan Gisselquist, Ph.D.
18
//              Gisselquist Technology, LLC
19
//
20
////////////////////////////////////////////////////////////////////////////////
21
//
22
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
23
//
24
// This program is free software (firmware): you can redistribute it and/or
25
// modify it under the terms of  the GNU General Public License as published
26
// by the Free Software Foundation, either version 3 of the License, or (at
27
// your option) any later version.
28
//
29
// This program is distributed in the hope that it will be useful, but WITHOUT
30
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
31
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
// for more details.
33
//
34
// You should have received a copy of the GNU General Public License along
35
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
36
// target there if the PDF file isn't present.)  If not, see
37
// <http://www.gnu.org/licenses/> for a copy.
38
//
39
// License:     GPL, v3, as defined and found on www.gnu.org,
40
//              http://www.gnu.org/licenses/gpl.html
41
//
42
//
43
////////////////////////////////////////////////////////////////////////////////
44
//
45
//
46
#include <stdio.h>
47
#include <string.h>
48
#include <stdint.h>
49
#include <assert.h>
50
#include "memsim.h"
51
 
52
MEMSIM::MEMSIM(const unsigned int nwords, const unsigned int delay) {
53
        unsigned int    nxt;
54
        for(nxt=1; nxt < nwords; nxt<<=1)
55
                ;
56
        m_len = nxt; m_mask = nxt-1;
57
        m_mem = new BUSW[m_len];
58
 
59
        m_delay = delay;
60
        for(m_delay_mask=1; m_delay_mask < delay; m_delay_mask<<=1)
61
                ;
62
        m_fifo_ack  = new int[m_delay_mask];
63
        m_fifo_data = new BUSW[m_delay_mask];
64
        for(unsigned i=0; i<m_delay_mask; i++)
65
                m_fifo_ack[i] = 0;
66
        m_delay_mask-=1;
67
        m_head = 0; m_tail = (m_head - delay)&m_delay_mask;
68
}
69
 
70
MEMSIM::~MEMSIM(void) {
71
        delete[]        m_mem;
72
}
73
 
74
void    MEMSIM::load(const char *fname) {
75
        FILE    *fp;
76
        unsigned int    nr;
77
 
78
        fp = fopen(fname, "r");
79
        if (!fp) {
80
                fprintf(stderr, "Could not open/load file \'%s\'\n",
81
                        fname);
82
                perror("O/S Err:");
83
                fprintf(stderr, "\tInitializing memory with zero instead.\n");
84
                nr = 0;
85
        } else {
86
                nr = fread(m_mem, sizeof(BUSW), m_len, fp);
87
                fclose(fp);
88
 
89
                if (nr != m_len) {
90
                        fprintf(stderr, "Only read %d of %d words\n",
91
                                nr, m_len);
92
                        fprintf(stderr, "\tFilling the rest with zero.\n");
93
                }
94
        }
95
 
96
        for(; nr<m_len; nr++)
97
                m_mem[nr] = 0l;
98
}
99
 
100
void    MEMSIM::load(const unsigned int addr, const char *buf, const size_t len) {
101
        memcpy(&m_mem[addr], buf, len);
102
}
103
 
104
void    MEMSIM::apply(const uchar wb_cyc, const uchar wb_stb, const uchar wb_we,
105
                        const BUSW wb_addr, const BUSW wb_data, const uchar wb_sel,
106
                        unsigned char &o_ack, unsigned char &o_stall, BUSW &o_data) {
107
        unsigned        sel = 0;
108
 
109
        if (wb_sel&0x8)
110
                sel |= 0x0ff000000;
111
        if (wb_sel&0x4)
112
                sel |= 0x000ff0000;
113
        if (wb_sel&0x2)
114
                sel |= 0x00000ff00;
115
        if (wb_sel&0x1)
116
                sel |= 0x0000000ff;
117
        m_head++; m_tail = (m_head - m_delay)&m_delay_mask;
118
        m_head&=m_delay_mask;
119
        o_ack = m_fifo_ack[m_tail];
120
        o_data= m_fifo_data[m_tail];
121
 
122
        m_fifo_ack[ m_head] = 0;
123
        m_fifo_data[m_head] = 0;
124
 
125
        o_stall= 0;
126
        if ((wb_cyc)&&(wb_stb)) {
127
                if (wb_we) {
128
                        if (sel == 0xffffffffu)
129
                                m_mem[wb_addr & m_mask] = wb_data;
130
                        else {
131
                                uint32_t memv = m_mem[wb_addr & m_mask];
132
                                memv &= ~sel;
133
                                memv |= (wb_data & sel);
134
                                m_mem[wb_addr & m_mask] = memv;
135
                        }
136
                }
137
                m_fifo_ack[m_head] = 1;
138
                m_fifo_data[m_head] = m_mem[wb_addr & m_mask];
139
#ifdef  DEBUG
140
                printf("MEMBUS %s[%08x] = %08x\n",
141
                        (wb_we)?"W":"R",
142
                        wb_addr&m_mask,
143
                        m_mem[wb_addr&m_mask]);
144
#endif
145
                // o_ack  = 1;
146
        }
147
 
148
#ifdef  DEBUG
149
        if (o_ack) {
150
                printf("MEMBUS -- ACK %s 0x%08x - 0x%08x\n",
151
                        (wb_we)?"WRITE":"READ ",
152
                        wb_addr, o_data);
153
        }
154
#endif
155
}
156
 
157
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.