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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [bench/] [cpp/] [memsim.cpp] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 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 Tecnology, LLC
15
//
16
////////////////////////////////////////////////////////////////////////////////
17
//
18
// Copyright (C) 2015, 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
#include <stdio.h>
41
#include <assert.h>
42
#include "memsim.h"
43
 
44
MEMSIM::MEMSIM(const unsigned int nwords) {
45
        unsigned int    nxt;
46
        for(nxt=1; nxt < nwords; nxt<<=1)
47
                ;
48
        m_len = nxt; m_mask = nxt-1;
49
        m_mem = new BUSW[m_len];
50
}
51
 
52
MEMSIM::~MEMSIM(void) {
53
        delete[]        m_mem;
54
}
55
 
56
void    MEMSIM::load(const char *fname) {
57
        FILE    *fp;
58
        unsigned int    nr;
59
 
60
        fp = fopen(fname, "r");
61
        if (!fp) {
62
                fprintf(stderr, "Could not open/load file \'%s\'\n",
63
                        fname);
64
                perror("O/S Err:");
65
                fprintf(stderr, "\tInitializing memory with zero instead.\n");
66
                nr = 0;
67
        } else {
68
                nr = fread(m_mem, sizeof(BUSW), m_len, fp);
69
                fclose(fp);
70
 
71
                if (nr != m_len) {
72
                        fprintf(stderr, "Only read %d of %d words\n",
73
                                nr, m_len);
74
                        fprintf(stderr, "\tFilling the rest with zero.\n");
75
                }
76
        }
77
 
78
        for(; nr<m_len; nr++)
79
                m_mem[nr] = 0l;
80
}
81
 
82
void    MEMSIM::apply(const unsigned char wb_cyc,
83
                        const unsigned char wb_stb, const unsigned char wb_we,
84
                        const BUSW wb_addr, const BUSW wb_data,
85
                        unsigned char &o_ack, unsigned char &o_stall, BUSW &o_data) {
86
        if ((wb_cyc)&&(wb_stb)) {
87
                if (wb_we)
88
                        m_mem[wb_addr & m_mask] = wb_data;
89
                o_ack  = 1;
90
                o_stall= 0;
91
                o_data = m_mem[wb_addr & m_mask];
92
 
93
                /*
94
                printf("MEMBUS -- ACK %s 0x%08x - 0x%08x\n",
95
                        (wb_we)?"WRITE":"READ",
96
                        wb_addr, o_data);
97
                */
98
        } else {
99
                o_ack   = 0;
100
                o_stall = 0;
101
        }
102
}
103
 
104
 

powered by: WebSVN 2.1.0

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