1 |
5 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: ziprun.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: XuLA2 board
|
6 |
|
|
//
|
7 |
|
|
// Purpose: To load a program for the ZipCPU into memory.
|
8 |
|
|
//
|
9 |
|
|
// Steps:
|
10 |
|
|
// 1. Halt and reset the CPU
|
11 |
|
|
// 2. Load memory
|
12 |
|
|
// 3. Clear the cache
|
13 |
|
|
// 4. Clear any registers
|
14 |
|
|
// 5. Set the PC to point to the FPGA local memory
|
15 |
|
|
// THIS DOES NOT START THE PROGRAM!! The CPU is left in the halt state.
|
16 |
|
|
// To actually start the program, execute a ./wbregs cpu 0. (Actually,
|
17 |
|
|
// any value between 0x0 and 0x1f will work, the difference being what
|
18 |
|
|
// register you will be able to inspect while the CPU is running.)
|
19 |
|
|
//
|
20 |
|
|
//
|
21 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
22 |
|
|
// Gisselquist Technology, LLC
|
23 |
|
|
//
|
24 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
//
|
26 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
27 |
|
|
//
|
28 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
29 |
|
|
// modify it under the terms of the GNU General Public License as published
|
30 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
31 |
|
|
// your option) any later version.
|
32 |
|
|
//
|
33 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
35 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
36 |
|
|
// for more details.
|
37 |
|
|
//
|
38 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
39 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
40 |
|
|
//
|
41 |
|
|
//
|
42 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
43 |
|
|
//
|
44 |
|
|
//
|
45 |
|
|
//
|
46 |
|
|
#include <stdio.h>
|
47 |
|
|
#include <stdlib.h>
|
48 |
|
|
#include <unistd.h>
|
49 |
|
|
#include <strings.h>
|
50 |
|
|
#include <ctype.h>
|
51 |
|
|
#include <string.h>
|
52 |
|
|
#include <signal.h>
|
53 |
|
|
#include <assert.h>
|
54 |
|
|
|
55 |
|
|
#include "usbi.h"
|
56 |
|
|
#include "port.h"
|
57 |
|
|
#include "regdefs.h"
|
58 |
|
|
|
59 |
|
|
FPGA *m_fpga;
|
60 |
|
|
|
61 |
|
|
int main(int argc, char **argv) {
|
62 |
|
|
FILE *fp;
|
63 |
|
|
int nr, pos=0;
|
64 |
|
|
const int BUFLN = 128;
|
65 |
|
|
FPGA::BUSW *buf = new FPGA::BUSW[BUFLN];
|
66 |
|
|
int skp=0, port = FPGAPORT;
|
67 |
|
|
bool use_usb = true;
|
68 |
|
|
|
69 |
|
|
skp=1;
|
70 |
|
|
for(int argn=0; argn<argc-skp; argn++) {
|
71 |
|
|
if (argv[argn+skp][0] == '-') {
|
72 |
|
|
if (argv[argn+skp][1] == 'u')
|
73 |
|
|
use_usb = true;
|
74 |
|
|
else if (argv[argn+skp][1] == 'p') {
|
75 |
|
|
use_usb = false;
|
76 |
|
|
if (isdigit(argv[argn+skp][2]))
|
77 |
|
|
port = atoi(&argv[argn+skp][2]);
|
78 |
|
|
}
|
79 |
|
|
skp++; argn--;
|
80 |
|
|
} else
|
81 |
|
|
argv[argn] = argv[argn+skp];
|
82 |
|
|
} argc -= skp;
|
83 |
|
|
|
84 |
|
|
if (use_usb)
|
85 |
|
|
m_fpga = new FPGA(new USBI());
|
86 |
|
|
else
|
87 |
|
|
m_fpga = new FPGA(new NETCOMMS(FPGAHOST, port));
|
88 |
|
|
|
89 |
|
|
if ((argc<=0)||(access(argv[0],R_OK)!=0)) {
|
90 |
|
|
printf("Usage: ziprun obj-file\n");
|
91 |
|
|
printf("\n"
|
92 |
|
|
"\tziprun loads the object file into memory, resets the CPU, and leaves it\n"
|
93 |
|
|
"\tin a halted state ready to start running the object file.\n");
|
94 |
|
|
exit(-1);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// FPGAOPEN(m_fpga);
|
98 |
|
|
|
99 |
|
|
printf("Halting the CPU\n");
|
100 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT);
|
101 |
|
|
|
102 |
|
|
fp = fopen(argv[0], "r");
|
103 |
|
|
if (fp == NULL) {
|
104 |
|
|
fprintf(stderr, "Could not open: %s\n", argv[0]);
|
105 |
|
|
exit(-1);
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
try {
|
109 |
|
|
pos = RAMBASE;
|
110 |
|
|
while((nr=fread(buf, sizeof(FPGA::BUSW), BUFLN, fp))>0) {
|
111 |
|
|
// printf("Writing %4d values, pos = %08x\n", nr, pos);
|
112 |
|
|
m_fpga->writei(pos, nr, buf);
|
113 |
|
|
// printf("\tWritten\n");
|
114 |
|
|
pos += nr;
|
115 |
|
|
} printf("Successfully wrote %04x (%6d) words into memory\n",
|
116 |
|
|
pos-RAMBASE, pos-RAMBASE);
|
117 |
|
|
m_fpga->readio(R_ZIPCTRL);
|
118 |
|
|
|
119 |
|
|
// Do we want to zero out all other RAM addresses?
|
120 |
|
|
#define ZERO_RAM
|
121 |
|
|
#ifdef ZERO_RAM
|
122 |
|
|
unsigned int MAXRAM=2*RAMBASE;
|
123 |
|
|
for(int i=0; i<BUFLN; i++)
|
124 |
|
|
buf[i] = 0;
|
125 |
|
|
printf("***********************\n");
|
126 |
|
|
while(pos < (int)MAXRAM-BUFLN-1) {
|
127 |
|
|
m_fpga->writei(pos, BUFLN, buf);
|
128 |
|
|
m_fpga->readio(R_ZIPCTRL);
|
129 |
|
|
pos += BUFLN;
|
130 |
|
|
} m_fpga->writei(pos, MAXRAM-pos-1, buf);
|
131 |
|
|
pos += MAXRAM-pos-1;
|
132 |
|
|
|
133 |
|
|
m_fpga->usleep(500);
|
134 |
|
|
printf("Zerod rest of RAM - to %06x\n", pos);
|
135 |
|
|
#endif
|
136 |
|
|
} catch(BUSERR a) {
|
137 |
|
|
fprintf(stderr, "BUS Err at address 0x%08x\n", a.addr);
|
138 |
|
|
fprintf(stderr, "... is your program too long for this memory?\n");
|
139 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
|
140 |
|
|
exit(-2);
|
141 |
|
|
}
|
142 |
|
|
try {
|
143 |
|
|
m_fpga->readio(R_ZIPCTRL);
|
144 |
|
|
} catch(BUSERR a) {
|
145 |
|
|
fprintf(stderr, "Bus-Err? (%08x)\n", a.addr);
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
// Clear any buffers
|
149 |
|
|
printf("Clearing the cache\n");
|
150 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
|
151 |
|
|
|
152 |
|
|
// printf("Clearing all registers to zero, PC regs to MEMBASE\n");
|
153 |
|
|
// Clear all registers to zero
|
154 |
|
|
for(int i=0; i<32; i++) {
|
155 |
|
|
unsigned int v;
|
156 |
|
|
try {
|
157 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i);
|
158 |
|
|
m_fpga->readio(R_ZIPCTRL);
|
159 |
|
|
} catch(BUSERR a) {
|
160 |
|
|
fprintf(stderr, "Bus-ERR while trying to set CPUCTRL to %x\n", CPU_HALT|i);
|
161 |
|
|
}
|
162 |
|
|
try {
|
163 |
|
|
if ((i&0x0f)==0x0f)
|
164 |
|
|
m_fpga->writeio(R_ZIPDATA, RAMBASE);
|
165 |
|
|
else
|
166 |
|
|
m_fpga->writeio(R_ZIPDATA, 0);
|
167 |
|
|
// printf("REG[%2x] <= %08x\n", i, ((i&0x0f)==0x0f)?RAMBASE:0);
|
168 |
|
|
// m_fpga->readio(R_ZIPDATA);
|
169 |
|
|
// printf("\t= %08x\n", m_fpga->readio(R_ZIPDATA));
|
170 |
|
|
} catch(BUSERR a) {
|
171 |
|
|
fprintf(stderr, "Bus-ERR while trying to clear reg %x\n", i);
|
172 |
|
|
}
|
173 |
|
|
} for(int i=32; i<32+16; i++) {
|
174 |
|
|
try {
|
175 |
|
|
if (i==33)
|
176 |
|
|
continue; // Don't start the watchdog
|
177 |
|
|
if (i==34)
|
178 |
|
|
continue; // Don't start the flash cache
|
179 |
|
|
if (i==39)
|
180 |
|
|
continue; // Jiffies don't clear, don't set the intrupt
|
181 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i);
|
182 |
|
|
m_fpga->writeio(R_ZIPDATA, 0);
|
183 |
|
|
} catch (BUSERR a) {
|
184 |
|
|
fprintf(stderr, "Bus-ERR while trying to clear peripheral %d\n", i);
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
printf("Starting CPU\n");
|
189 |
|
|
try {
|
190 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sCC); // Start in interrupt mode
|
191 |
|
|
m_fpga->writeio(R_ZIPDATA, 0x000);
|
192 |
|
|
printf("SCC <= 0x%08x\n", m_fpga->readio(R_ZIPDATA));
|
193 |
|
|
} catch (BUSERR a) {
|
194 |
|
|
fprintf(stderr, "Bus Err while trying to set CC register\n");
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
try {
|
198 |
|
|
m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sPC);
|
199 |
|
|
printf("CPU <= 0x%08x\n", m_fpga->readio(R_ZIPCTRL));
|
200 |
|
|
m_fpga->writeio(R_ZIPDATA, RAMBASE); // Start at the base of RAM
|
201 |
|
|
printf("SPC <= 0x%08x\n", m_fpga->readio(R_ZIPDATA));
|
202 |
|
|
} catch (BUSERR a) {
|
203 |
|
|
fprintf(stderr, "Bus Err while trying to set PC register\n");
|
204 |
|
|
}
|
205 |
|
|
printf("PC set to start at %08x\n", m_fpga->readio(R_ZIPDATA));
|
206 |
|
|
// m_fpga->writeio(R_ZIPCTRL, CPU_GO); // Release the CPU to start
|
207 |
|
|
|
208 |
|
|
delete m_fpga;
|
209 |
|
|
}
|
210 |
|
|
|