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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [bench/] [cpp/] [memsim.cpp] - Diff between revs 36 and 69

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 36 Rev 69
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Filename:    memsim.cpp
// Filename:    memsim.cpp
//
//
// Project:     Zip CPU -- a small, lightweight, RISC CPU core
// Project:     Zip CPU -- a small, lightweight, RISC CPU core
//
//
// Purpose:     This creates a memory like device to act on a WISHBONE bus.
// Purpose:     This creates a memory like device to act on a WISHBONE bus.
//              It doesn't exercise the bus thoroughly, but does give some
//              It doesn't exercise the bus thoroughly, but does give some
//              exercise to the bus to see whether or not the bus master
//              exercise to the bus to see whether or not the bus master
//              can control it.
//              can control it.
//
//
//
//
// Creator:     Dan Gisselquist, Ph.D.
// Creator:     Dan Gisselquist, Ph.D.
//              Gisselquist Tecnology, LLC
//              Gisselquist Technology, LLC
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Copyright (C) 2015, Gisselquist Technology, LLC
// Copyright (C) 2015, Gisselquist Technology, LLC
//
//
// This program is free software (firmware): you can redistribute it and/or
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// your option) any later version.
//
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// for more details.
//
//
// You should have received a copy of the GNU General Public License along
// You should have received a copy of the GNU General Public License along
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
// target there if the PDF file isn't present.)  If not, see
// target there if the PDF file isn't present.)  If not, see
// <http://www.gnu.org/licenses/> for a copy.
// <http://www.gnu.org/licenses/> for a copy.
//
//
// License:     GPL, v3, as defined and found on www.gnu.org,
// License:     GPL, v3, as defined and found on www.gnu.org,
//              http://www.gnu.org/licenses/gpl.html
//              http://www.gnu.org/licenses/gpl.html
//
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdio.h>
#include <assert.h>
#include <assert.h>
#include "memsim.h"
#include "memsim.h"
 
 
MEMSIM::MEMSIM(const unsigned int nwords) {
MEMSIM::MEMSIM(const unsigned int nwords) {
        unsigned int    nxt;
        unsigned int    nxt;
        for(nxt=1; nxt < nwords; nxt<<=1)
        for(nxt=1; nxt < nwords; nxt<<=1)
                ;
                ;
        m_len = nxt; m_mask = nxt-1;
        m_len = nxt; m_mask = nxt-1;
        m_mem = new BUSW[m_len];
        m_mem = new BUSW[m_len];
}
}
 
 
MEMSIM::~MEMSIM(void) {
MEMSIM::~MEMSIM(void) {
        delete[]        m_mem;
        delete[]        m_mem;
}
}
 
 
void    MEMSIM::load(const char *fname) {
void    MEMSIM::load(const char *fname) {
        FILE    *fp;
        FILE    *fp;
        unsigned int    nr;
        unsigned int    nr;
 
 
        fp = fopen(fname, "r");
        fp = fopen(fname, "r");
        if (!fp) {
        if (!fp) {
                fprintf(stderr, "Could not open/load file \'%s\'\n",
                fprintf(stderr, "Could not open/load file \'%s\'\n",
                        fname);
                        fname);
                perror("O/S Err:");
                perror("O/S Err:");
                fprintf(stderr, "\tInitializing memory with zero instead.\n");
                fprintf(stderr, "\tInitializing memory with zero instead.\n");
                nr = 0;
                nr = 0;
        } else {
        } else {
                nr = fread(m_mem, sizeof(BUSW), m_len, fp);
                nr = fread(m_mem, sizeof(BUSW), m_len, fp);
                fclose(fp);
                fclose(fp);
 
 
                if (nr != m_len) {
                if (nr != m_len) {
                        fprintf(stderr, "Only read %d of %d words\n",
                        fprintf(stderr, "Only read %d of %d words\n",
                                nr, m_len);
                                nr, m_len);
                        fprintf(stderr, "\tFilling the rest with zero.\n");
                        fprintf(stderr, "\tFilling the rest with zero.\n");
                }
                }
        }
        }
 
 
        for(; nr<m_len; nr++)
        for(; nr<m_len; nr++)
                m_mem[nr] = 0l;
                m_mem[nr] = 0l;
}
}
 
 
void    MEMSIM::apply(const unsigned int clk, const unsigned char wb_cyc,
void    MEMSIM::apply(const unsigned int clk, const unsigned char wb_cyc,
                        const unsigned char wb_stb, const unsigned char wb_we,
                        const unsigned char wb_stb, const unsigned char wb_we,
                        const BUSW wb_addr, const BUSW wb_data,
                        const BUSW wb_addr, const BUSW wb_data,
                        unsigned char &o_ack, unsigned char &o_stall, BUSW &o_data) {
                        unsigned char &o_ack, unsigned char &o_stall, BUSW &o_data) {
        if ((wb_cyc)&&(wb_stb)&&(clk)) {
        if ((wb_cyc)&&(wb_stb)&&(clk)) {
                if (wb_we)
                if (wb_we)
                        m_mem[wb_addr & m_mask] = wb_data;
                        m_mem[wb_addr & m_mask] = wb_data;
                o_ack  = 1;
                o_ack  = 1;
                o_stall= 0;
                o_stall= 0;
                o_data = m_mem[wb_addr & m_mask];
                o_data = m_mem[wb_addr & m_mask];
 
 
                /*
                /*
                printf("MEMBUS -- ACK %s 0x%08x - 0x%08x\n",
                printf("MEMBUS -- ACK %s 0x%08x - 0x%08x\n",
                        (wb_we)?"WRITE":"READ",
                        (wb_we)?"WRITE":"READ",
                        wb_addr, o_data);
                        wb_addr, o_data);
                */
                */
        } else if (clk) {
        } else if (clk) {
                o_ack   = 0;
                o_ack   = 0;
                o_stall = 0;
                o_stall = 0;
        }
        }
}
}
 
 
 
 
 
 

powered by: WebSVN 2.1.0

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