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

Subversion Repositories cpu_lecture

Compare Revisions

  • This comparison shows the changes necessary to convert path
    /
    from Rev 4 to Rev 5
    Reverse comparison

Rev 4 → Rev 5

/cpu_lecture/trunk/tools/make_mem.cc
3,12 → 3,13
#include "stdint.h"
#include "string.h"
 
const char * hex_file = 0;
const char * vhdl_file = 0;
uint8_t buffer[0x10000]; // 64 k is max. for Intel hex.
uint8_t slice [0x10000]; // 16 k is max. for Xilinx bram
 
uint8_t buffer[0x10000];
 
//-----------------------------------------------------------------------------
//
// get a byte (from cp pointing into Intel hex file).
//
uint32_t
get_byte(const char * cp)
{
19,6 → 20,8
return value;
}
//-----------------------------------------------------------------------------
//
// read an Intel hex file into buffer
void
read_file(FILE * in)
{
52,54 → 55,97
}
}
//-----------------------------------------------------------------------------
void
write_vector(FILE * out, bool odd, uint32_t mem, uint32_t v)
//
// copy a slice from buffer into slice.
// buffer is organized as 32-bit x items.
// slice is organized as bits x items.
//
void copy_slice(uint32_t slice_num, uint32_t port_bits, uint32_t mem_bits)
{
const uint8_t * base = buffer;
assert(mem_bits == 0x1000 || mem_bits == 0x4000);
 
// total memory is 2 even bytes, 2 odd bytes, 2 even bytes, ...
//
if (odd) base += 2;
const uint32_t items = mem_bits/port_bits;
const uint32_t mask = (1 << port_bits) - 1;
const uint8_t * src = buffer;
 
// total memory is 4 kByte organized into 8 memories.
// thus each of the 16 vectors covers 256 bytes.
//
base += v*256;
memset(slice, 0, sizeof(slice));
 
// memories 0 and 1 are the low byte of the opcode while
// memories 2 and 3 are the high byte.
//
if (mem >= 2) ++base;
for (uint32_t i = 0; i < items; ++i)
{
// read one 32-bit value;
const uint32_t v0 = *src++;
const uint32_t v1 = *src++;
const uint32_t v2 = *src++;
const uint32_t v3 = *src++;
const uint32_t v = (v3 << 24 |
v2 << 16 |
v1 << 8 |
v0 ) >> (slice_num*port_bits) & mask;
 
const char * px = odd ? "po" : "pe";
fprintf(out, "constant %s_%u_%2.2X : BIT_VECTOR := X\"", px, mem, v);
for (int32_t d = 63; d >= 0; --d)
{
uint32_t q = base[4*d];
if (mem & 1) q >>= 4; // high nibble
else q &= 0x0F; // low nibble
fprintf(out, "%X", q);
}
if (port_bits == 16)
{
assert(v < 0x10000);
slice[2*i] = v;
slice[2*i + 1] = v >> 8;
}
else if (port_bits == 8)
{
assert(v < 0x100);
slice[i] = v;
}
else if (port_bits == 4)
{
assert(v < 0x10);
slice[i >> 1] |= v << (4*(i & 1));
}
else if (port_bits == 2)
{
assert(v < 0x04);
slice[i >> 2] |= v << (2*(i & 3));
}
else if (port_bits == 1)
{
assert(v < 0x02);
slice[i >> 3] |= v << ((i & 7));
}
else assert(0 && "Bad aspect ratio.");
}
}
//-----------------------------------------------------------------------------
//
// write one initialization vector
//
void
write_vector(FILE * out, uint32_t mem, uint32_t vec, const uint8_t * data)
{
fprintf(out, "constant p%u_%2.2X : BIT_VECTOR := X\"", mem, vec);
for (int32_t d = 31; d >= 0; --d)
fprintf(out, "%2.2X", data[d]);
 
fprintf(out, "\";\r\n");
}
//-----------------------------------------------------------------------------
//
// write one memory
//
void
write_mem(FILE * out, bool odd, uint32_t mem)
write_mem(FILE * out, uint32_t mem, uint32_t bytes)
{
const char * px = odd ? "po" : "pe";
fprintf(out, "-- content of p_%u --------------------------------------"
"--------------------------------------------\r\n", mem);
 
fprintf(out, "-- content of %s_%u --------------------------------------"
"--------------------------------------------\r\n", px, mem);
const uint8_t * src = slice;
for (uint32_t v = 0; v < bytes/32; ++v)
write_vector(out, mem, v, src + 32*v);
 
for (uint32_t v = 0; v < 16; ++v)
write_vector(out, odd, mem, v);
 
fprintf(out, "\r\n");
}
//-----------------------------------------------------------------------------
//
// write the entire memory_contents file.
//
void
write_file(FILE * out)
write_file(FILE * out, uint32_t bits)
{
fprintf(out,
"\r\n"
109,11 → 155,13
"package prog_mem_content is\r\n"
"\r\n");
 
for (uint32_t m = 0; m < 4; ++m)
write_mem(out, false, m);
const uint32_t mems = 16/bits;
 
for (uint32_t m = 0; m < 4; ++m)
write_mem(out, true, m);
for (uint32_t m = 0; m < 2*mems; ++m)
{
copy_slice(m, bits, 0x1000);
write_mem(out, m, 0x200);
}
 
fprintf(out,
"end prog_mem_content;\r\n"
123,9 → 171,22
int
main(int argc, char * argv[])
{
if (argc > 1) hex_file = argv[1];
if (argc > 2) vhdl_file = argv[2];
uint32_t bits = 4;
const char * prog = *argv++; --argc;
 
if (argc && !strcmp(*argv, "-1")) { bits = 1; ++argv; --argc; }
else if (argc && !strcmp(*argv, "-2")) { bits = 2; ++argv; --argc; }
else if (argc && !strcmp(*argv, "-4")) { bits = 4; ++argv; --argc; }
else if (argc && !strcmp(*argv, "-8")) { bits = 8; ++argv; --argc; }
else if (argc && !strcmp(*argv, "-16")) { bits = 16; ++argv; --argc; }
 
const char * hex_file = 0;
const char * vhdl_file = 0;
 
if (argc) { hex_file = *argv++; --argc; }
if (argc) { vhdl_file = *argv++; --argc; }
assert(argc == 0);
 
FILE * in = stdin;
if (hex_file) in = fopen(hex_file, "r");
assert(in);
134,7 → 195,7
 
FILE * out = stdout;
if (vhdl_file) out = fopen(vhdl_file, "w");
write_file(out);
write_file(out, bits);
assert(out);
}
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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