Skip to content

Commit

Permalink
Minor code refactoring
Browse files Browse the repository at this point in the history
- All derved backend class will have same name, #ifdefs will select
the correct backend.
- Add getTargetName method to backend class.
- Correct UART addresses in hello.s example.
  • Loading branch information
saursin committed Jul 20, 2021
1 parent 5493915 commit c35cdf4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
36 changes: 13 additions & 23 deletions sim/AtomSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ std::string signature_file = "";
// Include Backend
#include "Backend_AtomBones.hpp"

// Backend name
const std::string AtomSimBackend = "AtomBones";

// Backend Object
Backend_AtomBones *bkend;

// Default mem size for atomBones
const unsigned long int default_mem_size = 134217728 + 3 + 1; // 128MB (Code & Data) + 3 Bytes (Serial IO) + 1 (To make word access possible)
// 128MB (Code & Data) + 3 Bytes (Serial IO) + 1 (To make word access possible on address 0x08000000)
const unsigned long int default_mem_size = (128*1024*1024) + 3 + 1;
unsigned long int mem_size = default_mem_size;
#endif


// Backend Object
Backend_AtomSim *bkend;

/**
* @brief Exit AtomSim
*
Expand All @@ -110,7 +110,7 @@ void ExitAtomSim(std::string message, bool exit_with_error)
std::cout << message << "\n";

// Destroy backend
bkend->~Backend_AtomBones();
bkend->~Backend_AtomSim();

// ===== Exit =====
if(exit_with_error)
Expand Down Expand Up @@ -238,7 +238,7 @@ void run(long unsigned int cycles)
int main(int argc, char **argv)
{
if (verbose_flag)
std::cout << "AtomSim [" << AtomSimBackend << "]\n";
std::cout << "AtomSim [" << bkend->getTargetName() << "]\n";

// Initialize verilator
Verilated::commandArgs(argc, argv);
Expand All @@ -248,7 +248,7 @@ int main(int argc, char **argv)

// Create a new backend instance
#ifdef TARGET_ATOMBONES
bkend = new Backend_AtomBones(ifile, default_mem_size);
bkend = new Backend_AtomSim(ifile, default_mem_size);
#endif

if(trace_enabled == true)
Expand Down Expand Up @@ -342,8 +342,6 @@ int main(int argc, char **argv)
else
std::cout << "Trace was not enabled \n";
}
// ============== BACKEND SPECIFIC COMMANDS ==================
#ifdef TARGET_ATOMBONES
else if(token[0] == "mem")
{
if(token.size()<2)
Expand All @@ -358,25 +356,17 @@ int main(int argc, char **argv)
else // Decimal Number
addr = std::stoi(token[1], nullptr, 10);

printf("%08x : %02x %02x %02x %02x\n", addr, bkend->mem->fetchByte(addr),
bkend->mem->fetchByte(addr+1),bkend->mem->fetchByte(addr+2), bkend->mem->fetchByte(addr+3));
uint32_t data = bkend->getMemContents(addr);
printf("%08x : %02x %02x %02x %02x\n", addr, (data & 0x000000ff), (data & 0x0000ff00)>>8, (data & 0x00ff0000)>>16, (data & 0xff000000)>>24);
}
}
else if(token[0] == "dumpmem")
{
if(token.size()<2)
throwError("CMD1", "\"dumpmem\" command expects filename as argument\n");

std::vector<std::string> fcontents;
for(unsigned int i=0; i<bkend->mem->size-4; i+=4)
{
char hex [30];
sprintf(hex, "0x%08x\t:\t0x%08x", i, bkend->mem->fetchWord(i));
fcontents.push_back(hex);
}
fWrite(fcontents, token[1]);
else
bkend->dumpmem(token[1]);
}
#endif

else
{
Expand Down
27 changes: 27 additions & 0 deletions sim/Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Backend
};

public:

/**
* @brief Get the Target Name
* @return std::string
*/
virtual std::string getTargetName() = 0;

/**
* @brief reset the backend
*/
Expand Down Expand Up @@ -138,6 +145,26 @@ class Backend
}
}

/**
* @brief Dump contents of memory into a file
* OVERRIDE THIS IN ANY DERIVED CLASSES
* @param file
*/
void dumpmem(std::string file)
{
throwError("", "Memory dumps not supported in current target");
}

/**
* @brief Get contents of a memory location
* OVERRIDE THIS IN ANY DERIVED CLASSES
*/
uint32_t getMemContents(uint32_t addr)
{
throwError("", "Viewing memory content not suppoted in current target");
return 0;
}

/**
* @brief Tick for one cycle
*
Expand Down
40 changes: 37 additions & 3 deletions sim/Backend_AtomBones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class Memory
* Backend class encapsulates the data
* probing and printing operations
*/
class Backend_AtomBones: public Backend<VAtomBones>
class Backend_AtomSim: public Backend<VAtomBones>
{
public:
/**
Expand All @@ -267,7 +267,7 @@ class Backend_AtomBones: public Backend<VAtomBones>
/**
* @brief Construct a new Backend object
*/
Backend_AtomBones(std::string ifile, unsigned long mem_size)
Backend_AtomSim(std::string ifile, unsigned long mem_size)
{
// Construct Testbench object
tb = new Testbench<VAtomBones>();
Expand Down Expand Up @@ -297,12 +297,17 @@ class Backend_AtomBones: public Backend<VAtomBones>
/**
* @brief Destroy the Backend object
*/
~Backend_AtomBones()
~Backend_AtomSim()
{
delete tb;
delete mem;
}

std::string getTargetName()
{
return "AtomBones";
}

void serviceMemoryRequest()
{
// Clear all ack signals
Expand Down Expand Up @@ -497,4 +502,33 @@ class Backend_AtomBones: public Backend<VAtomBones>
}
prev_tx_we = cur_tx_we;
}

/**
* @brief Dump contents of memoy into a file
* @param file
*/
void dumpmem(std::string file)
{
if(mem->size < 1*1024*1024) // 1MB
{
std::vector<std::string> fcontents;
for(unsigned int i=0; i<mem->size-4; i+=4)
{
char hex [30];
sprintf(hex, "0x%08x\t:\t0x%08x", i, mem->fetchWord(i));
fcontents.push_back(hex);
}
fWrite(fcontents, std::string(default_dump_dir)+"/"+file);
}
else
throwError("","Option not available due to excessive memory size (>1MB)\n", false);
}

/**
* @brief Get contents of a memory location
*/
uint32_t getMemContents(uint32_t addr)
{
return mem->fetchWord(addr);
}
};
4 changes: 2 additions & 2 deletions sw/examples/hello/hello.s
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.global main
.text

.equ TX_ADDRESS, 0x00014001
.equ TX_ACK_ADDRESS, 0x00014002
.equ TX_ADDRESS, 0x08000001
.equ TX_ACK_ADDRESS, 0x08000002

main:
la a0, msg
Expand Down
4 changes: 2 additions & 2 deletions sw/lib/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ ENTRY(_start)
/* MEMORY LAYOUT */
MEMORY
{
ROM (rx) : ORIGIN = 0x00000000, LENGTH = 64M /* 64 kb @ 0x0*/
RAM (rwx): ORIGIN = 0x04000000, LENGTH = 64M /* 16 kb @ 0x10000 (65536)*/
ROM (rx) : ORIGIN = 0x00000000, LENGTH = 64M /* 64 MB @ 0x0*/
RAM (rwx): ORIGIN = 0x04000000, LENGTH = 64M /* 64 MB @ 0x10000 (0x04000000)*/
}

SECTIONS
Expand Down

0 comments on commit c35cdf4

Please sign in to comment.