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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [ziprun.cpp] - Blame information for rev 11

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

Line No. Rev Author Line
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
        printf("Halting the CPU\n");
98 11 dgisselq
        m_fpga->usleep(5);
99 5 dgisselq
        m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT);
100
 
101
        fp = fopen(argv[0], "r");
102
        if (fp == NULL) {
103
                fprintf(stderr, "Could not open: %s\n", argv[0]);
104
                exit(-1);
105
        }
106
 
107
        try {
108
                pos = RAMBASE;
109
                while((nr=fread(buf, sizeof(FPGA::BUSW), BUFLN, fp))>0) {
110
                        // printf("Writing %4d values, pos = %08x\n", nr, pos);
111
                        m_fpga->writei(pos, nr, buf);
112
                        // printf("\tWritten\n");
113
                        pos += nr;
114
                } printf("Successfully  wrote %04x (%6d) words into memory\n",
115
                                pos-RAMBASE, pos-RAMBASE);
116
                m_fpga->readio(R_ZIPCTRL);
117
 
118
        // Do we want to zero out all other RAM addresses?
119
#define ZERO_RAM
120
#ifdef  ZERO_RAM
121
                unsigned int    MAXRAM=2*RAMBASE;
122
                for(int i=0; i<BUFLN; i++)
123
                        buf[i] = 0;
124
                printf("***********************\n");
125
                while(pos < (int)MAXRAM-BUFLN-1) {
126
                        m_fpga->writei(pos, BUFLN, buf);
127
                        m_fpga->readio(R_ZIPCTRL);
128
                        pos += BUFLN;
129
                } m_fpga->writei(pos, MAXRAM-pos-1, buf);
130
                pos += MAXRAM-pos-1;
131
 
132
                m_fpga->usleep(500);
133
                printf("Zerod rest of RAM - to %06x\n", pos);
134
#endif
135
        } catch(BUSERR a) {
136
                fprintf(stderr, "BUS Err at address 0x%08x\n", a.addr);
137
                fprintf(stderr, "... is your program too long for this memory?\n");
138
                m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
139
                exit(-2);
140
        }
141
        try {
142
                m_fpga->readio(R_ZIPCTRL);
143
        } catch(BUSERR a) {
144
                fprintf(stderr, "Bus-Err? (%08x)\n", a.addr);
145
        }
146
 
147
        // Clear any buffers
148
        printf("Clearing the cache\n");
149
        m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
150
 
151 11 dgisselq
        printf("Clearing all registers to zero, PC regs to MEMBASE\n");
152 5 dgisselq
        // Clear all registers to zero
153
        for(int i=0; i<32; i++) {
154
                try {
155
                        m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i);
156
                        m_fpga->readio(R_ZIPCTRL);
157
                } catch(BUSERR a) {
158
                        fprintf(stderr, "Bus-ERR while trying to set CPUCTRL to %x\n", CPU_HALT|i);
159
                }
160
                try {
161
                        if ((i&0x0f)==0x0f)
162 11 dgisselq
                                m_fpga->writeio(R_ZIPDATA, RAMBASE);
163 5 dgisselq
                        else
164 11 dgisselq
                                m_fpga->writeio(R_ZIPDATA, 0);
165 5 dgisselq
                        // printf("REG[%2x] <= %08x\n", i, ((i&0x0f)==0x0f)?RAMBASE:0);
166
                        // m_fpga->readio(R_ZIPDATA);
167
                        // printf("\t= %08x\n", m_fpga->readio(R_ZIPDATA));
168
                } catch(BUSERR a) {
169
                        fprintf(stderr, "Bus-ERR while trying to clear reg %x\n", i);
170
                }
171 11 dgisselq
        }
172
 
173
 
174
        printf("Clearing all peripherals\n");
175
        for(int i=32; i<32+16; i++) {
176 5 dgisselq
                try {
177
                if (i==33)
178
                        continue; // Don't start the watchdog
179
                if (i==34)
180
                        continue; // Don't start the flash cache
181
                if (i==39)
182
                        continue; // Jiffies don't clear, don't set the intrupt
183
                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i);
184
                m_fpga->writeio(R_ZIPDATA, 0);
185
                } catch (BUSERR a) {
186
                        fprintf(stderr, "Bus-ERR while trying to clear peripheral %d\n", i);
187
                }
188
        }
189
 
190
        printf("Starting CPU\n");
191
        try {
192
                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sCC);   // Start in interrupt mode
193
                m_fpga->writeio(R_ZIPDATA, 0x000);
194
                printf("SCC <= 0x%08x\n", m_fpga->readio(R_ZIPDATA));
195
        } catch (BUSERR a) {
196
                fprintf(stderr, "Bus Err while trying to set CC register\n");
197
        }
198
 
199
        try {
200
                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sPC);
201
                printf("CPU <= 0x%08x\n", m_fpga->readio(R_ZIPCTRL));
202
                m_fpga->writeio(R_ZIPDATA, RAMBASE);    // Start at the base of RAM
203
                printf("SPC <= 0x%08x\n", m_fpga->readio(R_ZIPDATA));
204
        } catch (BUSERR a) {
205
                fprintf(stderr, "Bus Err while trying to set PC register\n");
206
        }
207
        printf("PC set to start at %08x\n", m_fpga->readio(R_ZIPDATA));
208
//      m_fpga->writeio(R_ZIPCTRL, CPU_GO);     // Release the CPU to start
209
 
210
        delete  m_fpga;
211
}
212
 

powered by: WebSVN 2.1.0

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