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

Subversion Repositories cpu_lecture

[/] [cpu_lecture/] [trunk/] [html/] [33_Listing_of_make_mem.cc.html] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jsauermann
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2
"http://www.w3.org/TR/html4/strict.dtd">
3
<HTML>
4
<HEAD>
5
<TITLE>html/Listing_of_make_mem.cc</TITLE>
6
<META NAME="generator" CONTENT="HTML::TextToHTML v2.46">
7
<LINK REL="stylesheet" TYPE="text/css" HREF="lecture.css">
8
</HEAD>
9
<BODY>
10
<P><table class="ttop"><th class="tpre"><a href="32_Listing_of_hello.c.html">Previous Lesson</a></th><th class="ttop"><a href="toc.html">Table of Content</a></th><th class="tnxt"><a href="34_Listing_of_end_conv.cc.html">Next Lesson</a></th></table>
11
<hr>
12
 
13
<H1><A NAME="section_1">33 LISTING OF make_mem.cc</A></H1>
14
 
15
<pre class="vhdl">
16
 
17
  1     #include "assert.h"
18
  2     #include "stdio.h"
19
  3     #include "stdint.h"
20
  4     #include "string.h"
21
  5
22
  6     const char * hex_file = 0;
23
  7     const char * vhdl_file = 0;
24
  8
25
  9     uint8_t buffer[0x10000];
26
 10
27
 11     //-----------------------------------------------------------------------------
28
 12     uint32_t
29
 13     get_byte(const char *  cp)
30
 14     {
31
 15     uint32_t value;
32
 16     const char cc[3] = { cp[0], cp[1], 0 };
33
 17     const int cnt = sscanf(cc, "%X", &value);
34
 18        assert(cnt == 1);
35
 19        return value;
36
 20     }
37
 21     //-----------------------------------------------------------------------------
38
 22     void
39
 23     read_file(FILE * in)
40
 24     {
41
 25        memset(buffer, 0xFF, sizeof(buffer));
42
 26     char line[200];
43
 27        for (;;)
44
 28            {
45
 29              const char * s = fgets(line, sizeof(line) - 2, in);
46
 30              if (s == 0)   return;
47
 31              assert(*s++ == ':');
48
 32              const uint32_t len     = get_byte(s);
49
 33              const uint32_t ah      = get_byte(s + 2);
50
 34              const uint32_t al      = get_byte(s + 4);
51
 35              const uint32_t rectype = get_byte(s + 6);
52
 36              const char * d = s + 8;
53
 37              const uint32_t addr = ah << 8 | al;
54
 38
55
 39              uint32_t csum = len + ah + al + rectype;
56
 40              assert((addr + len) <= 0x10000);
57
 41              for (uint32_t l = 0; l < len; ++l)
58
 42                  {
59
 43                    const uint32_t byte = get_byte(d);
60
 44                    d += 2;
61
 45                    buffer[addr + l] = byte;
62
 46                    csum += byte;
63
 47                  }
64
 48
65
 49              csum = 0xFF & -csum;
66
 50              const uint32_t sum = get_byte(d);
67
 51              assert(sum == csum);
68
 52            }
69
 53     }
70
 54     //-----------------------------------------------------------------------------
71
 55     void
72
 56     write_vector(FILE * out, bool odd, uint32_t mem, uint32_t v)
73
 57     {
74
 58     const uint8_t * base = buffer;
75
 59
76
 60        // total memory is 2 even bytes, 2 odd bytes, 2 even bytes, ...
77
 61        //
78
 62        if (odd)   base += 2;
79
 63
80
 64        // total memory is 4 kByte organized into 8 memories.
81
 65        // thus each of the 16 vectors covers 256 bytes.
82
 66        //
83
 67        base += v*256;
84
 68
85
 69        // memories 0 and 1 are the low byte of the opcode while
86
 70        // memories 2 and 3 are the high byte.
87
 71        //
88
 72        if (mem >= 2)   ++base;
89
 73
90
 74     const char * px = odd ? "po" : "pe";
91
 75        fprintf(out, "constant %s_%u_%2.2X : BIT_VECTOR := X\"", px, mem, v);
92
 76        for (int32_t d = 63; d >= 0; --d)
93
 77            {
94
 78              uint32_t q = base[4*d];
95
 79              if (mem & 1)   q >>= 4;     // high nibble
96
 80              else           q &= 0x0F;   // low nibble
97
 81              fprintf(out, "%X", q);
98
 82            }
99
 83
100
 84        fprintf(out, "\";\r\n");
101
 85     }
102
 86     //-----------------------------------------------------------------------------
103
 87     void
104
 88     write_mem(FILE * out, bool odd, uint32_t mem)
105
 89     {
106
 90     const char * px = odd ? "po" : "pe";
107
 91
108
 92        fprintf(out, "-- content of %s_%u --------------------------------------"
109
 93                     "--------------------------------------------\r\n", px, mem);
110
 94
111
 95        for (uint32_t v = 0; v < 16; ++v)
112
 96            write_vector(out, odd, mem, v);
113
 97
114
 98        fprintf(out, "\r\n");
115
 99     }
116
100     //-----------------------------------------------------------------------------
117
101     void
118
102     write_file(FILE * out)
119
103     {
120
104        fprintf(out,
121
105     "\r\n"
122
106     "library IEEE;\r\n"
123
107     "use IEEE.STD_LOGIC_1164.all;\r\n"
124
108     "\r\n"
125
109     "package prog_mem_content is\r\n"
126
110     "\r\n");
127
111
128
112        for (uint32_t m = 0; m < 4; ++m)
129
113            write_mem(out, false, m);
130
114
131
115        for (uint32_t m = 0; m < 4; ++m)
132
116            write_mem(out, true,  m);
133
117
134
118        fprintf(out,
135
119     "end prog_mem_content;\r\n"
136
120     "\r\n");
137
121     }
138
122     //-----------------------------------------------------------------------------
139
123     int
140
124     main(int argc, char * argv[])
141
125     {
142
126        if (argc > 1)   hex_file = argv[1];
143
127        if (argc > 2)   vhdl_file = argv[2];
144
128
145
129     FILE * in = stdin;
146
130        if (hex_file)   in = fopen(hex_file, "r");
147
131        assert(in);
148
132        read_file(in);
149
133        fclose(in);
150
134
151
135     FILE * out = stdout;
152
136        if (vhdl_file)   out = fopen(vhdl_file, "w");
153
137        write_file(out);
154
138        assert(out);
155
139     }
156
140     //-----------------------------------------------------------------------------
157
<pre class="filename">
158
tools/make_mem.cc
159
</pre></pre>
160
<P>
161
 
162
<P><hr><BR>
163
<table class="ttop"><th class="tpre"><a href="32_Listing_of_hello.c.html">Previous Lesson</a></th><th class="ttop"><a href="toc.html">Table of Content</a></th><th class="tnxt"><a href="34_Listing_of_end_conv.cc.html">Next Lesson</a></th></table>
164
</BODY>
165
</HTML>

powered by: WebSVN 2.1.0

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