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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sim/] [verilator/] [memsim.cpp] - Blame information for rev 209

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 204 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    memsim.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU core
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
//
13
// Creator:     Dan Gisselquist, Ph.D.
14
//              Gisselquist Technology, LLC
15
//
16
////////////////////////////////////////////////////////////////////////////////
17
//
18
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
19
//
20
// This program is free software (firmware): you can redistribute it and/or
21
// modify it under the terms of  the GNU General Public License as published
22
// by the Free Software Foundation, either version 3 of the License, or (at
23
// your option) any later version.
24
//
25
// This program is distributed in the hope that it will be useful, but WITHOUT
26
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
27
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28
// for more details.
29
//
30
// You should have received a copy of the GNU General Public License along
31
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
32
// target there if the PDF file isn't present.)  If not, see
33
// <http://www.gnu.org/licenses/> for a copy.
34
//
35
// License:     GPL, v3, as defined and found on www.gnu.org,
36
//              http://www.gnu.org/licenses/gpl.html
37
//
38
//
39
////////////////////////////////////////////////////////////////////////////////
40
//
41
//
42
#include <stdio.h>
43
#include <assert.h>
44
#include <string.h>
45
#include <stdint.h>
46
#include "byteswap.h"
47
#include "memsim.h"
48
 
49
MEMSIM::MEMSIM(const unsigned int nwords) {
50
        unsigned int    nxt;
51
 
52
        for(nxt=1; nxt < nwords; nxt<<=1)
53
                ;
54
        m_len = nxt; m_mask = nxt-1;
55
        m_mem = new BUSW[m_len];
56
        m_nxt_ack = 0;
57
}
58
 
59
MEMSIM::~MEMSIM(void) {
60
        delete[]        m_mem;
61
}
62
 
63
void    MEMSIM::load(const char *fname) {
64
        FILE    *fp;
65
        unsigned int    nr;
66
 
67
        fp = fopen(fname, "r");
68
        if (!fp) {
69
                fprintf(stderr, "Could not open/load file \'%s\'\n",
70
                        fname);
71
                perror("O/S Err:");
72
                fprintf(stderr, "\tInitializing memory with zero instead.\n");
73
                nr = 0;
74
        } else {
75
                nr = fread(m_mem, sizeof(BUSW), m_len, fp);
76
                fclose(fp);
77
 
78
                if (nr != m_len) {
79
                        fprintf(stderr, "Only read %d of %d words\n",
80
                                nr, m_len);
81
                        fprintf(stderr, "\tFilling the rest with zero.\n");
82
                }
83
        }
84
 
85
        for(; nr<m_len; nr++)
86
                m_mem[nr] = 0l;
87
}
88
 
89
void    MEMSIM::load(const unsigned addr, const char *buf, const unsigned len) {
90
        memcpy(&m_mem[addr], buf, len);
91
        byteswapbuf((len>>2), &m_mem[addr]);
92
}
93
 
94
void    MEMSIM::apply(const uchar wb_cyc,
95
                        const uchar wb_stb, const uchar wb_we,
96
                        const BUSW wb_addr, const BUSW wb_data, const uchar wb_sel,
97
                        uchar &o_ack, uchar &o_stall, BUSW &o_data) {
98
        unsigned        sel = 0;
99
 
100
        if (wb_sel&0x8)
101
                sel |= 0x0ff000000;
102
        if (wb_sel&0x4)
103
                sel |= 0x000ff0000;
104
        if (wb_sel&0x2)
105
                sel |= 0x00000ff00;
106
        if (wb_sel&0x1)
107
                sel |= 0x0000000ff;
108
 
109
        o_ack = (m_nxt_ack) && (wb_cyc);
110
        o_data= m_nxt_data;
111
        m_nxt_data = wb_data;
112
        m_nxt_ack  = 0;
113
        o_stall= 0;
114
        if ((wb_cyc)&&(wb_stb)) {
115
                if (wb_we) {
116
                        if (sel == 0xffffffffu)
117
                                m_mem[wb_addr & m_mask] = wb_data;
118
                        else {
119
                                uint32_t memv = m_mem[wb_addr & m_mask];
120
                                memv &= ~sel;
121
                                memv |= (wb_data & sel);
122
                                m_mem[wb_addr & m_mask] = memv;
123
                        }
124
                }
125
                m_nxt_ack = 1;
126
                m_nxt_data = m_mem[wb_addr & m_mask];
127
                // o_ack  = 1;
128
 
129
                {
130
                        extern FILE *gbl_dbgfp;
131
                        if (gbl_dbgfp) {
132
                                if (wb_we) fprintf(gbl_dbgfp, "MEMSIM::MEM[%08x] = %08x\n", wb_addr&m_mask, wb_data);
133
                                else
134
                                        fprintf(gbl_dbgfp, "MEMSIM::BUS = MEM[%08x] = %08x\n", wb_addr&m_mask, m_nxt_data);
135
                        }
136
                }
137
        }
138
}
139
 
140
 

powered by: WebSVN 2.1.0

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