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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [monitor/] [monitor/] [common/] [load.c] - Blame information for rev 269

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

Line No. Rev Author Line
1 59 hellwig
/*
2 201 hellwig
 * load.c -- load S-records from serial line
3 59 hellwig
 */
4
 
5
 
6
#include "common.h"
7
#include "stdarg.h"
8
#include "romlib.h"
9
#include "load.h"
10
#include "serial.h"
11
#include "cpu.h"
12
#include "mmu.h"
13
 
14
 
15
#define NUM_TRIES       10
16
#define WAIT_DELAY      350000
17
 
18
#define SYN             ((unsigned char) 's')
19
#define ACK             ((unsigned char) 'a')
20
 
21
#define LINE_SIZE       520
22
 
23
 
24
static Byte line[LINE_SIZE];
25
 
26
 
27
static Word getByte(int index) {
28
  Word hi, lo;
29
 
30
  hi = line[index + 0];
31
  if (hi >= '0' && hi <= '9') {
32
    hi -= '0';
33
  } else
34
  if (hi >= 'A' && hi <= 'F') {
35
    hi -= 'A' - 10;
36
  } else
37
  if (hi >= 'a' && hi <= 'f') {
38
    hi -= 'a' - 10;
39
  } else {
40 257 hellwig
    return (Word) -1;
41 59 hellwig
  }
42
  lo = line[index + 1];
43
  if (lo >= '0' && lo <= '9') {
44
    lo -= '0';
45
  } else
46
  if (lo >= 'A' && lo <= 'F') {
47
    lo -= 'A' - 10;
48
  } else
49
  if (lo >= 'a' && lo <= 'f') {
50
    lo -= 'a' - 10;
51
  } else {
52 257 hellwig
    return (Word) -1;
53 59 hellwig
  }
54
  return (hi << 4) | lo;
55
}
56
 
57
 
58
static void serialOut(int serno, Word c) {
59
  if (serno == 0) {
60
    ser0out(c);
61
  } else {
62
    ser1out(c);
63
  }
64
}
65
 
66
 
67
static int serialChk(int serno) {
68
  if (serno == 0) {
69
    return ser0inchk();
70
  } else {
71
    return ser1inchk();
72
  }
73
}
74
 
75
 
76
static Word serialIn(int serno) {
77
  if (serno == 0) {
78
    return ser0in();
79
  } else {
80
    return ser1in();
81
  }
82
}
83
 
84
 
85 201 hellwig
void load(int serno, Bool start) {
86 59 hellwig
  int i, j;
87
  Bool run;
88
  int type;
89
  int count;
90
  Word chksum;
91
  Word addr;
92
  Byte b;
93
 
94
  printf("Trying to connect to load server...\n");
95
  for (i = 0; i < NUM_TRIES; i++) {
96
    serialOut(serno, SYN);
97
    for (j = 0; j < WAIT_DELAY; j++) {
98
      if (serialChk(serno) != 0) {
99
        break;
100
      }
101
    }
102
    if (j < WAIT_DELAY) {
103
      break;
104
    }
105
    printf("Request timed out...\n");
106
  }
107
  if (i == NUM_TRIES ||
108
      serialIn(serno) != ACK) {
109
    printf("Unable to establish connection to load server.\n");
110
    return;
111
  }
112
  serialOut(serno, ACK);
113
  printf("Connected to load server.\n");
114
  run = true;
115
  while (run) {
116
    serialOut(serno, 'r');
117
    for (i = 0; i < LINE_SIZE; i++) {
118
      line[i] = serialIn(serno);
119
      if (line[i] == '\n') {
120
        break;
121
      }
122
    }
123
    if (i == LINE_SIZE) {
124
      printf("Error: too many characters in S-record!\n");
125
      break;
126
    }
127
    line[i] = '\0';
128
    printf("%s\n", line);
129
    if (line[0] != 'S') {
130
      printf("Error: malformed S-record!\n");
131
      break;
132
    }
133
    type = line[1];
134
    count = getByte(2);
135
    if (i != 2 * count + 4) {
136
      printf("Error: inconsistent byte count in S-record!\n");
137
      break;
138
    }
139
    chksum = 0;
140
    for (j = 2; j < i; j += 2) {
141
      chksum += getByte(j);
142
    }
143
    if ((chksum & 0xFF) != 0xFF) {
144
      printf("Error: wrong checksum in S-record!\n");
145
      break;
146
    }
147
    switch (type) {
148
      case '0':
149
        /* S0 record: header (skip) */
150
        break;
151
      case '1':
152
        /* S1 record: 2 byte load address + data (load data) */
153
        addr = (getByte( 4) <<  8) |
154
               (getByte( 6) <<  0);
155
        addr |= 0xC0000000;
156
        for (j = 0; j < count - 3; j++) {
157
          b = getByte(2 * j + 8);
158
          mmuWriteByte(addr + j, b);
159
        }
160
        break;
161
      case '2':
162
        /* S2 record: 3 byte load address + data (load data) */
163
        addr = (getByte( 4) << 16) |
164
               (getByte( 6) <<  8) |
165
               (getByte( 8) <<  0);
166
        addr |= 0xC0000000;
167
        for (j = 0; j < count - 4; j++) {
168
          b = getByte(2 * j + 10);
169
          mmuWriteByte(addr + j, b);
170
        }
171
        break;
172
      case '3':
173
        /* S3 record: 4 byte load address + data (load data) */
174
        addr = (getByte( 4) << 24) |
175
               (getByte( 6) << 16) |
176
               (getByte( 8) <<  8) |
177
               (getByte(10) <<  0);
178
        addr |= 0xC0000000;
179
        for (j = 0; j < count - 5; j++) {
180
          b = getByte(2 * j + 12);
181
          mmuWriteByte(addr + j, b);
182
        }
183
        break;
184
      case '5':
185
        /* S5 record: record count (skip) */
186
        break;
187
      case '7':
188
        /* S7 record: 4 byte start address (set PC, stop loading) */
189
        addr = (getByte( 4) << 24) |
190
               (getByte( 6) << 16) |
191
               (getByte( 8) <<  8) |
192
               (getByte(10) <<  0);
193
        addr |= 0xC0000000;
194
        cpuSetPC(addr);
195
        run = false;
196
        break;
197
      case '8':
198
        /* S8 record: 3 byte start address (set PC, stop loading) */
199
        addr = (getByte( 4) << 16) |
200
               (getByte( 6) <<  8) |
201
               (getByte( 8) <<  0);
202
        addr |= 0xC0000000;
203
        cpuSetPC(addr);
204
        run = false;
205
        break;
206
      case '9':
207
        /* S9 record: 2 byte start address (set PC, stop loading) */
208
        addr = (getByte( 4) <<  8) |
209
               (getByte( 6) <<  0);
210
        addr |= 0xC0000000;
211
        cpuSetPC(addr);
212
        run = false;
213
        break;
214
      default:
215
        /* unknown type of S-record */
216
        printf("Error: unknown type of S-record!\n");
217
        run = false;
218
        break;
219
    }
220
  }
221
  serialOut(serno, 'q');
222
  printf("Connection to load server closed.\n");
223 201 hellwig
  if (start) {
224
    cpuRun();
225
  }
226 59 hellwig
}

powered by: WebSVN 2.1.0

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