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

Subversion Repositories cpu_lecture

[/] [cpu_lecture/] [trunk/] [tools/] [make_mem.cc] - Blame information for rev 17

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

Line No. Rev Author Line
1 2 jsauermann
#include "assert.h"
2
#include "stdio.h"
3
#include "stdint.h"
4
#include "string.h"
5
 
6 5 jsauermann
uint8_t buffer[0x10000];    // 64 k is max. for Intel hex.
7
uint8_t slice [0x10000];    // 16 k is max. for Xilinx bram
8 2 jsauermann
 
9
//-----------------------------------------------------------------------------
10 5 jsauermann
//
11
// get a byte (from cp pointing into Intel hex file).
12
//
13 2 jsauermann
uint32_t
14
get_byte(const char *  cp)
15
{
16
uint32_t value;
17
const char cc[3] = { cp[0], cp[1], 0 };
18
const int cnt = sscanf(cc, "%X", &value);
19
   assert(cnt == 1);
20
   return value;
21
}
22
//-----------------------------------------------------------------------------
23 5 jsauermann
//
24
// read an Intel hex file into buffer
25 2 jsauermann
void
26
read_file(FILE * in)
27
{
28
   memset(buffer, 0xFF, sizeof(buffer));
29
char line[200];
30
   for (;;)
31
       {
32
         const char * s = fgets(line, sizeof(line) - 2, in);
33
         if (s == 0)   return;
34
         assert(*s++ == ':');
35
         const uint32_t len     = get_byte(s);
36
         const uint32_t ah      = get_byte(s + 2);
37
         const uint32_t al      = get_byte(s + 4);
38
         const uint32_t rectype = get_byte(s + 6);
39
         const char * d = s + 8;
40
         const uint32_t addr = ah << 8 | al;
41
 
42
         uint32_t csum = len + ah + al + rectype;
43
         assert((addr + len) <= 0x10000);
44
         for (uint32_t l = 0; l < len; ++l)
45
             {
46
               const uint32_t byte = get_byte(d);
47
               d += 2;
48
               buffer[addr + l] = byte;
49
               csum += byte;
50
             }
51
 
52
         csum = 0xFF & -csum;
53
         const uint32_t sum = get_byte(d);
54
         assert(sum == csum);
55
       }
56
}
57
//-----------------------------------------------------------------------------
58 5 jsauermann
//
59
// copy a slice from buffer into slice.
60
// buffer is organized as 32-bit x items.
61
// slice is organized as bits x items.
62
//
63
void copy_slice(uint32_t slice_num, uint32_t port_bits, uint32_t mem_bits)
64 2 jsauermann
{
65 5 jsauermann
   assert(mem_bits == 0x1000 || mem_bits == 0x4000);
66 2 jsauermann
 
67 5 jsauermann
const uint32_t items = mem_bits/port_bits;
68
const uint32_t mask = (1 << port_bits) - 1;
69
const uint8_t * src = buffer;
70 2 jsauermann
 
71 5 jsauermann
    memset(slice, 0, sizeof(slice));
72 2 jsauermann
 
73 5 jsauermann
    for (uint32_t i = 0; i < items; ++i)
74
        {
75
          // read one 32-bit value;
76
          const uint32_t v0 = *src++;
77
          const uint32_t v1 = *src++;
78
          const uint32_t v2 = *src++;
79
          const uint32_t v3 = *src++;
80
          const uint32_t v = (v3 << 24 |
81
                              v2 << 16 |
82
                              v1 <<  8 |
83
                              v0       ) >> (slice_num*port_bits) & mask;
84 2 jsauermann
 
85 5 jsauermann
          if (port_bits == 16)
86
             {
87
               assert(v < 0x10000);
88
               slice[2*i]     = v;
89
               slice[2*i + 1] = v >> 8;
90
             }
91
          else if (port_bits == 8)
92
             {
93
               assert(v < 0x100);
94
               slice[i] = v;
95
             }
96
          else if (port_bits == 4)
97
             {
98
               assert(v < 0x10);
99
               slice[i >> 1] |= v << (4*(i & 1));
100
             }
101
          else if (port_bits == 2)
102
             {
103
               assert(v < 0x04);
104
               slice[i >> 2] |= v << (2*(i & 3));
105
             }
106
          else if (port_bits == 1)
107
             {
108
               assert(v < 0x02);
109
               slice[i >> 3] |= v << ((i & 7));
110
             }
111
          else assert(0 && "Bad aspect ratio.");
112
        }
113
}
114
//-----------------------------------------------------------------------------
115
//
116
// write one initialization vector
117
//
118
void
119
write_vector(FILE * out, uint32_t mem, uint32_t vec, const uint8_t * data)
120
{
121
   fprintf(out, "constant p%u_%2.2X : BIT_VECTOR := X\"", mem, vec);
122
   for (int32_t d = 31; d >= 0; --d)
123
       fprintf(out, "%2.2X", data[d]);
124 2 jsauermann
 
125
   fprintf(out, "\";\r\n");
126
}
127
//-----------------------------------------------------------------------------
128 5 jsauermann
//
129
// write one memory
130
//
131 2 jsauermann
void
132 5 jsauermann
write_mem(FILE * out, uint32_t mem, uint32_t bytes)
133 2 jsauermann
{
134 5 jsauermann
   fprintf(out, "-- content of p_%u --------------------------------------"
135
                "--------------------------------------------\r\n", mem);
136 2 jsauermann
 
137 5 jsauermann
const uint8_t * src = slice;
138
   for (uint32_t v = 0; v < bytes/32; ++v)
139
       write_vector(out, mem, v, src + 32*v);
140 2 jsauermann
 
141
   fprintf(out, "\r\n");
142
}
143
//-----------------------------------------------------------------------------
144 5 jsauermann
//
145
// write the entire memory_contents file.
146
//
147 2 jsauermann
void
148 5 jsauermann
write_file(FILE * out, uint32_t bits)
149 2 jsauermann
{
150
   fprintf(out,
151
"\r\n"
152
"library IEEE;\r\n"
153
"use IEEE.STD_LOGIC_1164.all;\r\n"
154
"\r\n"
155
"package prog_mem_content is\r\n"
156
"\r\n");
157
 
158 5 jsauermann
const uint32_t mems = 16/bits;
159 2 jsauermann
 
160 5 jsauermann
   for (uint32_t m = 0; m < 2*mems; ++m)
161
       {
162
         copy_slice(m, bits, 0x1000);
163
         write_mem(out, m, 0x200);
164
       }
165 2 jsauermann
 
166
   fprintf(out,
167
"end prog_mem_content;\r\n"
168
"\r\n");
169
}
170
//-----------------------------------------------------------------------------
171
int
172
main(int argc, char * argv[])
173
{
174 5 jsauermann
uint32_t bits = 4;
175
const char * prog = *argv++;   --argc;
176 2 jsauermann
 
177 5 jsauermann
   if      (argc && !strcmp(*argv, "-1"))    { bits =  1;   ++argv;   --argc; }
178
   else if (argc && !strcmp(*argv, "-2"))    { bits =  2;   ++argv;   --argc; }
179
   else if (argc && !strcmp(*argv, "-4"))    { bits =  4;   ++argv;   --argc; }
180
   else if (argc && !strcmp(*argv, "-8"))    { bits =  8;   ++argv;   --argc; }
181
   else if (argc && !strcmp(*argv, "-16"))   { bits = 16;   ++argv;   --argc; }
182
 
183
const char * hex_file = 0;
184
const char * vhdl_file = 0;
185
 
186
   if (argc)   { hex_file  = *argv++;   --argc; }
187
   if (argc)   { vhdl_file = *argv++;   --argc; }
188
   assert(argc == 0);
189
 
190 2 jsauermann
FILE * in = stdin;
191
   if (hex_file)   in = fopen(hex_file, "r");
192
   assert(in);
193
   read_file(in);
194
   fclose(in);
195
 
196
FILE * out = stdout;
197
   if (vhdl_file)   out = fopen(vhdl_file, "w");
198 5 jsauermann
   write_file(out, bits);
199 2 jsauermann
   assert(out);
200
}
201
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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