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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [jtag/] [jp2.c] - Blame information for rev 1246

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

Line No. Rev Author Line
1 1246 markom
/* jp2-linux.c -- JTAG protocol via parallel port for linux
2
   Copyright(C) 2001 Marko Mlinar, markom@opencores.org
3
   Code for TCP/IP copied from gdb, by Chris Ziomkowski
4
 
5
This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
 
21
/* Establishes jtag proxy server and communicates with parallel
22
 port directly.  Requires root access. */
23
 
24
#include <stdio.h>
25
#include <ctype.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <unistd.h>
29
#include <stdarg.h>
30
 
31
/* Dirty way to include inb and outb from, but they say it is
32
   a standard one.  */
33
#include <asm/io.h>
34
#include <asm/system.h>
35
#include "mc.h"
36
#include "gdb.h"
37
#include "jp2.h"
38
 
39
static int base = 0x378; /* FIXME: We should detect the address. */
40
int err = 0;
41
int set_pc = 0;
42
int set_step = 0;
43
int waiting = 0;
44
 
45
/* Scan chain info. */
46
static int chain_addr_size[] = { 0,  32, 0,  0,  5,  32, 32};
47
static int chain_data_size[] = { 0,  32, 0,  32, 32, 32, 32};
48
static int chain_is_valid[]  = { 0,  1,  0,  1,  1,  1,   1};
49
static int chain_has_crc[]   = { 0,  1,  0,  1,  1,  1,   1};
50
static int chain_has_rw[]    = { 0,  1,  0,  0,  1,  1,   1};
51
 
52
/* Currently selected scan chain - just to prevent unnecessary
53
   transfers. */
54
static int current_chain = -1;
55
 
56
/* The chain that should be currently selected. */
57
static int dbg_chain = -1;
58
 
59
/* Crc of current read or written data.  */
60
static int crc_r, crc_w = 0;
61
 
62
/* Address of previous read */
63
static unsigned long prev_regno = 0;
64
 
65
/* Generates new crc, sending in new bit input_bit */
66
static unsigned long crc_calc(unsigned long crc, int input_bit) {
67
  unsigned long d = (input_bit&1) ? 0xfffffff : 0x0000000;
68
  unsigned long crc_32 = ((crc >> 31)&1) ? 0xfffffff : 0x0000000;
69
  crc <<= 1;
70
  return crc ^ (d ^ crc_32) & DBG_CRC_POLY;
71
}
72
 
73
/* Send a byte to parallel port. */
74
inline static void jp2_out(unsigned int value) {
75
#ifdef RTL_SIM
76
  time_t time;
77
  struct stat s;
78
  char buf[1000];
79
  FILE *fout;
80
  unsigned num_read;
81
  int r;
82
  fout = fopen(GDB_IN, "wt+");
83
  fprintf(fout, "F\n");
84
  fclose(fout);
85
  fout = fopen(GDB_OUT, "wt+");
86
  fprintf(fout, "%02X\n", value);
87
  fclose(fout);
88
error:
89
  fout = fopen(GDB_OUT, "rt");
90
  r = fscanf(fout,"%x", &num_read);
91
  fclose(fout);
92
  if(r == 0 || num_read !=(0x10 | value)) goto error;
93
#else
94
  outb(value, LPT_WRITE);
95
#endif /* RTL_SIM */
96
  if (!(value & 1)) debug("[%x%c]",(value & TDI_BIT) != 0,(value & TMS_BIT)?'^':'_');
97
  flush_debug();
98
}
99
 
100
/* Receive a byte from parallel port.  */
101
inline static unsigned char jp2_in() {
102
  int data;
103
#ifndef RTL_SIM
104
  data = inb(LPT_READ);
105
#ifdef TDO_INV
106
  data =(data & TDO_BIT) != TDO_BIT;
107
#else
108
  data =(data & TDO_BIT) == TDO_BIT;
109
#endif
110
#else
111
  FILE *fin = 0;
112
  char ch;
113
  time_t time;
114
  struct stat s;
115
  while(1) {
116
    fin = fopen(GDB_IN, "rt");
117
    if(fin == 0) continue;
118
    ch = fgetc(fin);
119
    if(ch != '0' && ch != '1') {
120
      fclose(fin);
121
      continue;
122
    } else break;
123
  }
124
  fclose(fin);
125
  data = ch == '1';
126
#endif /* !RTL_SIM */
127
  debug(" R%01X ", data);
128
  flush_debug();
129
  return data;
130
}
131
 
132
/* Writes TCLK=0, TRST=1, TMS=bit1, TDI=bit0
133
   and    TCLK=1, TRST=1, TMS=bit1, TDI=bit0 */
134
static inline void jp2_write_JTAG(unsigned char packet) {
135
  unsigned char data = TRST_BIT;
136
  if(packet & 1) data |= TDI_BIT;
137
  if(packet & 2) data |= TMS_BIT;
138
 
139
  jp2_out(data);
140
  JTAG_WAIT();
141
  crc_w = crc_calc(crc_w, packet&1);
142
 
143
  /* rise clock */
144
  jp2_out(data | TCLK_BIT);
145
  JTAG_WAIT();
146
}
147
 
148
/* Reads TDI.  */
149
static inline int jp2_read_JTAG() {
150
  int data;
151
  data = jp2_in();
152
  crc_r = crc_calc(crc_r, data);
153
  return data;
154
}
155
 
156
/* Writes bitstream.  LS bit first if len < 0, MS bit first if len > 0.  */
157
static inline void jp2_write_stream(ULONGEST stream, int len, int set_last_bit) {
158
  int i;
159
  if (len < 0) {
160
    len = -len;
161
    debug("\nwriteL(");
162
    for(i = 0; i < len - 1; i++)
163
      jp2_write_JTAG((stream >> i) & 1);
164
 
165
    if(set_last_bit) jp2_write_JTAG((stream >>(len - 1))& 1 | TMS);
166
    else jp2_write_JTAG((stream >>(len - 1))& 1);
167
  } else {
168
    debug("\nwrite(");
169
    for(i = 0; i < len - 1; i++)
170
      jp2_write_JTAG((stream >> (len - 1 - i)) & 1);
171
 
172
    if(set_last_bit) jp2_write_JTAG((stream >> 0) & 1 | TMS);
173
    else jp2_write_JTAG((stream >> 0)& 1);
174
  }
175
  debug(")\n");
176
}
177
 
178
/* Gets bitstream.  LS bit first if len < 0, MS bit first if len > 0.  */
179
inline static ULONGEST jp2_read_stream(unsigned long stream, int len, int set_last_bit) {
180
  int i;
181
  ULONGEST data = 0;
182
  if (len < 0) {
183
    debug("\nreadL(");
184
    for(i = 0; i < len - 1; i++) {
185
      jp2_write_JTAG((stream >> i) & 1);   /* LSB first */
186
      data |= jp2_read_JTAG() << i; /* LSB first */
187
    }
188
 
189
    if (set_last_bit) jp2_write_JTAG((stream >> (len - 1)) & 1 | TMS);
190
    else jp2_write_JTAG((stream >> (len - 1)) & 1);
191
    data |= jp2_read_JTAG() << (len - 1);
192
  } else {
193
    debug("\nread(");
194
    for(i = 0; i < len - 1; i++) {
195
      jp2_write_JTAG((stream >> (len - 1 - i)) & 1);   /* MSB first */
196
      data |= jp2_read_JTAG() << (len - 1 - i); /* MSB first */
197
    }
198
 
199
    if (set_last_bit) jp2_write_JTAG((stream >> 0) & 1 | TMS);
200
    else jp2_write_JTAG((stream >> 0) & 1);
201
    data |= jp2_read_JTAG() << 0;
202
  }
203
  debug(")\n");
204
  return data;
205
}
206
 
207
/* Sets scan chain.  */
208
void jtag_set_ir(int ir) {
209
  jp2_write_JTAG(TMS); /* SELECT_DR SCAN */
210
  jp2_write_JTAG(TMS); /* SELECT_IR SCAN */
211
 
212
  jp2_write_JTAG(0); /* CAPTURE_IR */
213
  jp2_write_JTAG(0); /* SHIFT_IR */
214
 
215
  /* write data, EXIT1_IR */
216
  jp2_write_stream(ir, -JI_SIZE, 1);
217
 
218
  jp2_write_JTAG(TMS); /* UPDATE_IR */
219
  jp2_write_JTAG(0); /* IDLE */
220
  current_chain = -1;
221
}
222
 
223
/* Resets JTAG
224
   Writes TRST=0
225
   and    TRST=1 */
226
static void jp2_reset_JTAG() {
227
  int i;
228
  debug2("\nreset(");
229
  jp2_out(0);
230
  JTAG_RETRY_WAIT();
231
  /* In case we don't have TRST reset it manually */
232
  for(i = 0; i < 8; i++) jp2_write_JTAG(TMS);
233
  jp2_out(TRST_BIT);
234
  JTAG_RETRY_WAIT();
235
  jp2_write_JTAG(0);
236
  debug2(")\n");
237
}
238
 
239
/* Resets JTAG, and sets DEBUG scan chain */
240
static int dbg_reset() {
241
  int err;
242
  jp2_reset_JTAG();
243
  /* select debug scan chain and stay in it forever */
244
  jtag_set_ir(JI_DEBUG);
245
  current_chain = -1;
246
  return DBG_ERR_OK;
247
}
248
 
249
/* counts retries and returns nonzero if we should abort */
250
/* TODO: dinamically adjust timings for jp2 */
251
static int retry_no = 0;
252
int retry_do() {
253
  int i, err;
254
  if (retry_no >= NUM_SOFT_RETRIES) {
255
    if ((err = dbg_reset())) return err;
256
  } else { /* quick reset */
257
    for(i = 0; i < 8; i++) jp2_write_JTAG(TMS);
258
    jp2_write_JTAG(0); /* go into IDLE state */
259
  }
260
  if (retry_no >= NUM_SOFT_RETRIES + NUM_HARD_RETRIES) {
261
    retry_no = 0;
262
    return 1;
263
  }
264
  retry_no++;
265
  return DBG_ERR_OK;
266
}
267
 
268
/* resets retry counter */
269
void retry_ok() {
270
  retry_no = 0;
271
}
272
 
273
/* Sets scan chain.  */
274
int dbg_set_chain(int chain) {
275
  int status, crc_generated, crc_read;
276
  dbg_chain = chain;
277
 
278
try_again:
279
  if (current_chain == chain) return DBG_ERR_OK;
280
  current_chain = -1;
281
  debug("\n");
282
  debug2("set_chain %i\n", chain);
283
  jp2_write_JTAG(TMS); /* SELECT_DR SCAN */
284
  jp2_write_JTAG(0); /* CAPTURE_DR */
285
  jp2_write_JTAG(0); /* SHIFT_DR */
286
 
287
  /* write data, EXIT1_DR */
288
  crc_w = 0;
289
  jp2_write_stream((chain & 0x7 | 0x8), DC_SIZE + 1, 0);
290
  jp2_write_stream(crc_w, DBG_CRC_SIZE, 0);
291
  crc_r = 0;
292
  status = jp2_read_stream(0, DC_STATUS_SIZE, 0);
293
  crc_generated = crc_r;
294
  crc_read = jp2_read_stream(0, DBG_CRC_SIZE, 1);
295
 
296
  /* invalid chain? */
297
  if (status == 0xf && crc_read == crc_generated) return DBG_ERR_INV_CHAIN;
298
 
299
  /* we should read 1101, otherwise retry */
300
  if (status != 0xd || crc_read != crc_generated) {
301
    if (retry_do()) goto try_again;
302
    else return DBG_ERR_CRC;
303
  }
304
 
305
  /* reset retry counter */
306
  retry_ok();
307
 
308
  jp2_write_JTAG(TMS); /* UPDATE_DR */
309
  jp2_write_JTAG(0); /* IDLE */
310
  current_chain = chain;
311
  return DBG_ERR_OK;
312
}
313
 
314
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
315
int dbg_command(unsigned long adr, int command, int len) {
316
  int status, crc_generated, crc_read;
317
 
318
try_again:
319
  dbg_set_chain(dbg_chain);
320
  debug("\n");
321
  debug2("wb_comm %i\n", command);
322
 
323
  /***** WRITEx *****/
324
  jp2_write_JTAG(TMS); /* SELECT_DR SCAN */
325
  jp2_write_JTAG(0); /* CAPTURE_DR */
326
  jp2_write_JTAG(0); /* SHIFT_DR */
327
 
328
  /* write data, EXIT1_DR */
329
  crc_w = 0;
330
  jp2_write_stream((command & 0x7), DC_SIZE + 1, 0);
331
  jp2_write_stream(adr, 32, 0);
332
  if (len >= 0) jp2_write_stream(len, 16, 0);
333
  jp2_write_stream(crc_w, DBG_CRC_SIZE, 0);
334
  crc_r = 0;
335
  status = jp2_read_stream(0, DC_STATUS_SIZE, 0);
336
  crc_generated = crc_r;
337
  crc_read = jp2_read_stream(0, DBG_CRC_SIZE, 1);
338
 
339
  /* we should read 1000, otherwise retry */
340
  if (status != 0x8 || crc_read != crc_generated) {
341
    if (retry_do()) goto try_again;
342
    else return DBG_ERR_CRC;
343
  }
344
  jp2_write_JTAG(TMS); /* UPDATE_DR */
345
  jp2_write_JTAG(0); /* IDLE */
346
 
347
  /* reset retry counter */
348
  retry_ok();
349
  return DBG_ERR_OK;
350
}
351
 
352
/* issues a burst read/write */
353
int dbg_go(int go_command, unsigned char *data, unsigned short len, int read) {
354
  int status, crc_generated, crc_read;
355
  int i;
356
 
357
try_again:
358
  dbg_set_chain(dbg_chain);
359
  debug("\n");
360
  debug2("go len = %d\n", len);
361
 
362
  jp2_write_JTAG(TMS); /* SELECT_DR SCAN */
363
  jp2_write_JTAG(0); /* CAPTURE_DR */
364
  jp2_write_JTAG(0); /* SHIFT_DR */
365
 
366
  /* write data, EXIT1_DR */
367
  crc_w = 0;
368
  jp2_write_stream(go_command, DC_SIZE + 1, 0);
369
  for (i = 0; i < len; i++)
370
    if (read) data[i] = jp2_read_stream(data[i], 8, 0);
371
    else jp2_write_stream(data[i], 8, 0);
372
  jp2_write_stream(crc_w, DBG_CRC_SIZE, 0);
373
  crc_r = 0;
374
  status = jp2_read_stream(0, DC_STATUS_SIZE, 0);
375
  crc_generated = crc_r;
376
  crc_read = jp2_read_stream(0, DBG_CRC_SIZE, 1);
377
 
378
  /* was there an bus error */
379
  if (status == 0x9 || crc_read == crc_generated) return DBG_ERR_BUSERR;
380
 
381
  /* we should read 1000, otherwise retry */
382
  if (status != 0x8 || crc_read != crc_generated) {
383
    if (retry_do()) goto try_again;
384
    else return DBG_ERR_CRC;
385
  }
386
  jp2_write_JTAG(TMS); /* UPDATE_DR */
387
  jp2_write_JTAG(0); /* IDLE */
388
 
389
  /* reset retry counter */
390
  retry_ok();
391
  return DBG_ERR_OK;
392
}
393
 
394
/* check the status */
395
int dbg_wb_status() {
396
  int status, crc_generated, crc_read;
397
  int wait_retries = 0;
398
 
399
try_again:
400
  dbg_set_chain(dbg_chain);
401
  debug("\n");
402
  debug2("status\n");
403
 
404
  jp2_write_JTAG(TMS); /* SELECT_DR SCAN */
405
  jp2_write_JTAG(0); /* CAPTURE_DR */
406
  jp2_write_JTAG(0); /* SHIFT_DR */
407
 
408
  /* write data, EXIT1_DR */
409
  crc_w = 0;
410
  jp2_write_stream(DI_WB_STATUS, DC_SIZE + 1, 0);
411
  jp2_write_stream(crc_w, DBG_CRC_SIZE, 0);
412
  crc_r = 0;
413
  status = jp2_read_stream(0, DC_STATUS_SIZE, 0);
414
  crc_generated = crc_r;
415
  crc_read = jp2_read_stream(0, DBG_CRC_SIZE, 1);
416
 
417
  /* is cycle still in progress */
418
  if (status == 0xc && crc_read == crc_generated) {
419
    jp2_write_JTAG(TMS); /* UPDATE_DR */
420
    jp2_write_JTAG(0); /* IDLE */
421
    JTAG_RETRY_WAIT();
422
    wait_retries++;
423
    if (wait_retries > NUM_ACCESS_RETRIES) goto try_again;
424
  }
425
 
426
  /* we should read 1000, otherwise retry */
427
  if (status != 0x8 || crc_read != crc_generated) {
428
    if (retry_do()) goto try_again;
429
    else return DBG_ERR_CRC;
430
  }
431
 
432
  jp2_write_JTAG(TMS); /* UPDATE_DR */
433
  jp2_write_JTAG(0); /* IDLE */
434
 
435
  /* reset retry counter */
436
  retry_ok();
437
  return DBG_ERR_OK;
438
}
439
 
440
/* read a block from wishbone */
441
int dbg_wb_read(unsigned long adr, int command, unsigned char *data, unsigned short len) {
442
  int err;
443
  if ((err = dbg_set_chain(DC_WISHBONE))) return err;
444
  if ((err = dbg_command(adr, command, len))) return err;
445
  if ((err = dbg_go(DI_WB_GO, data, len, 1))) return err;
446
  if ((err = dbg_wb_status())) return err;
447
  return DBG_ERR_OK;
448
}
449
 
450
/* write a block to wishbone */
451
int dbg_wb_write(unsigned long adr, int command, unsigned char *data, unsigned short len) {
452
  int err;
453
  if ((err = dbg_set_chain(DC_WISHBONE))) return err;
454
  if ((err = dbg_command(adr, command, len))) return err;
455
  if ((err = dbg_go(DI_WB_GO, data, len, 0))) return err;
456
  if ((err = dbg_wb_status())) return err;
457
  return DBG_ERR_OK;
458
}
459
 
460
/* read a register from cpu */
461
int dbg_cpu_read(unsigned long adr, int command, unsigned long *data) {
462
  int err;
463
  if ((err = dbg_set_chain(DC_CPU))) return err;
464
  if ((err = dbg_command(adr, command, -1))) return err;
465
  if ((err = dbg_go(DI_CPU_GO, (unsigned char*)data, 4, 1))) return err;
466
  return DBG_ERR_OK;
467
}
468
 
469
/* write a cpu register */
470
int dbg_cpu_write(unsigned long adr, int command, unsigned long data) {
471
  int err;
472
  if ((err = dbg_set_chain(DC_CPU))) return err;
473
  if ((err = dbg_command(adr, command, -1))) return err;
474
  if ((err = dbg_go(DI_CPU_GO, (unsigned char *)&data, 4, 0))) return err;
475
  return DBG_ERR_OK;
476
}
477
 
478
/* read a word from wishbone */
479
int dbg_wb_read32(unsigned long adr, unsigned long *data) {
480
  return dbg_wb_read(adr, DI_WB_READ32, (unsigned char*)data, 4);
481
}
482
 
483
/* write a word to wishbone */
484
int dbg_wb_write32(unsigned long adr, unsigned long data) {
485
  return dbg_wb_write(adr, DI_WB_WRITE32, (unsigned char*)&data, 4);
486
}
487
 
488
/* read a block from wishbone */
489
int dbg_wb_read_block_32(unsigned long adr, unsigned long *data, int len) {
490
  return dbg_wb_read(adr, DI_WB_READ32, (unsigned char*)data, len);
491
}
492
 
493
/* write a block to wishbone */
494
int dbg_wb_write_block32(unsigned long adr, unsigned long *data, int len) {
495
  return dbg_wb_write(adr, DI_WB_WRITE32, (unsigned char*)data, len);
496
}
497
 
498
/* read a register from cpu */
499
int dbg_cpu_read32(unsigned long adr, unsigned long *data) {
500
  return dbg_cpu_read(adr, DI_CPU_READ32, data);
501
}
502
 
503
/* read a register from cpu module */
504
int dbg_cpu_read_reg(unsigned long adr, unsigned long *data) {
505
  return dbg_cpu_read(adr, DI_CPU_READ_REG, data);
506
}
507
 
508
/* write a cpu register */
509
int dbg_cpu_write32(unsigned long adr, unsigned long data) {
510
  return dbg_cpu_write(adr, DI_CPU_WRITE32, data);
511
}
512
 
513
/* write a cpu module register */
514
int dbg_cpu_write_reg(unsigned long adr, unsigned long data) {
515
  return dbg_cpu_write(adr, DI_CPU_WRITE_REG, data);
516
}
517
 
518
void dbg_test() {
519
  int i;
520
  unsigned long npc, ppc, r1, insn, result;
521
#if 0
522
#define MC_BASE_ADD     0x60000000
523
#define MC_CSR_VAL      0x0f300300
524
#define MC_MASK_VAL     0x000000e0
525
#define FLASH_BASE_ADD  0x04000000
526
#define FLASH_TMS_VAL   0x0010a10a
527
#define SDRAM_BASE_ADD  0x00000000
528
#define SDRAM_TMS_VAL   0x07248230
529
 
530
  dbg_cpu_write_reg(4, 0x00000001);
531
 
532
  dbg_wb_write32(MC_BASE_ADD + MC_CSC(0),(((FLASH_BASE_ADD & 0xffff0000) >> 5) | 0x25));
533
  dbg_wb_write32(MC_BASE_ADD + MC_TMS(0), FLASH_TMS_VAL);
534
 
535
  dbg_wb_write32(MC_BASE_ADD + MC_BA_MASK, MC_MASK_VAL);
536
  dbg_wb_write32(MC_BASE_ADD + MC_CSR, MC_CSR_VAL);
537
 
538
  dbg_wb_write32(MC_BASE_ADD + MC_TMS(1), SDRAM_TMS_VAL);
539
  dbg_wb_write32(MC_BASE_ADD + MC_CSC(1),(((SDRAM_BASE_ADD & 0xffff0000) >> 5) | 0x0411));
540
 
541
  sleep(1);
542
#endif
543
 
544
#if 1
545
 
546
#define RAM_BASE 0x00000000
547
  /* Stall risc */
548
  dbg_cpu_write_reg(4, 0x00000001);
549
 
550
  dbg_wb_write32(RAM_BASE+0x00, 0x9c200000);                             /* l.addi  r1,r0,0x0*/
551
  dbg_wb_write32(RAM_BASE+0x04, 0x18400000+(RAM_BASE >> 16));            /* l.movhi r2,0x4000*/
552
  dbg_wb_write32(RAM_BASE+0x08, 0xa8420000+((RAM_BASE + 0x30) & 0xffff));/* l.ori   r2,r2,0x0*/
553
  dbg_wb_write32(RAM_BASE+0x0c, 0x9c210001);                             /* l.addi  r1,r1,1  */
554
  dbg_wb_write32(RAM_BASE+0x10, 0x9c210001);                             /* l.addi  r1,r1,1  */
555
  dbg_wb_write32(RAM_BASE+0x14, 0xd4020800);                             /* l.sw    0(r2),r1 */
556
  dbg_wb_write32(RAM_BASE+0x18, 0x9c210001);                             /* l.addi  r1,r1,1  */
557
  dbg_wb_write32(RAM_BASE+0x1c, 0x84620000);                             /* l.lwz   r3,0(r2) */
558
  dbg_wb_write32(RAM_BASE+0x20, 0x03fffffb);                             /* l.j     loop2    */
559
  dbg_wb_write32(RAM_BASE+0x24, 0xe0211800);                             /* l.add   r1,r1,r3 */
560
  dbg_wb_write32(RAM_BASE+0x24, 0xe0211800);                             /* l.add   r1,r1,r3 */
561
 
562
  dbg_cpu_write32((0 << 11) + 17, 0x01);  /* Enable exceptions */
563
  dbg_cpu_write32((6 << 11) + 20, 0x2000);  /* Trap causes stall */
564
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE);  /* Set PC */
565
  dbg_cpu_write32((6 << 11) + 16, 1 << 22);  /* Set step bit */
566
  for(i = 0; i < 10; i++) dbg_cpu_write_reg(4, 0x00000000);  /* 10x Unstall */
567
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
568
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
569
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
570
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
571
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 5);
572
  result = npc + ppc + r1;
573
 
574
  dbg_cpu_write32((6 << 11) + 16, 0);  /* Reset step bit */
575
  dbg_wb_read32(RAM_BASE + 0x24, &insn);  /* Set trap insn in delay slot */
576
  dbg_wb_write32(RAM_BASE + 0x24, 0x21000001);
577
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
578
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
579
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
580
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
581
  dbg_wb_write32(RAM_BASE + 0x24, insn);  /* Set back original insn */
582
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
583
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 8);
584
  result = npc + ppc + r1 + result;
585
 
586
  dbg_wb_read32(RAM_BASE + 0x20, &insn);  /* Set trap insn in place of branch insn */
587
  dbg_wb_write32(RAM_BASE + 0x20, 0x21000001);
588
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x0c);  /* Set PC */
589
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
590
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
591
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
592
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
593
  dbg_wb_write32(RAM_BASE + 0x20, insn);  /* Set back original insn */
594
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
595
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000024, 0x40000020, 11);
596
  result = npc + ppc + r1 + result;
597
 
598
  dbg_wb_read32(RAM_BASE + 0x1c, &insn);  /* Set trap insn before branch insn */
599
  dbg_wb_write32(RAM_BASE + 0x1c, 0x21000001);
600
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x20);  /* Set PC */
601
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
602
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
603
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
604
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
605
  dbg_wb_write32(RAM_BASE + 0x1c, insn);  /* Set back original insn */
606
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
607
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000020, 0x4000001c, 24);
608
  result = npc + ppc + r1 + result;
609
 
610
 
611
  dbg_wb_read32(RAM_BASE + 0x18, &insn);  /* Set trap insn behind lsu insn */
612
  dbg_wb_write32(RAM_BASE + 0x18, 0x21000001);
613
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x1c);  /* Set PC */
614
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
615
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
616
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
617
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
618
  dbg_wb_write32(RAM_BASE + 0x18, insn);  /* Set back original insn */
619
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
620
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000001c, 0x40000018, 49);
621
  result = npc + ppc + r1 + result;
622
 
623
  dbg_wb_read32(RAM_BASE + 0x1c, &insn);  /* Set trap insn very near previous one */
624
  dbg_wb_write32(RAM_BASE + 0x1c, 0x21000001);
625
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x18);  /* Set PC */
626
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
627
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
628
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
629
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
630
  dbg_wb_write32(RAM_BASE + 0x1c, insn);  /* Set back original insn */
631
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
632
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000020, 0x4000001c, 50);
633
  result = npc + ppc + r1 + result;
634
 
635
  dbg_wb_read32(RAM_BASE + 0x0c, &insn);  /* Set trap insn to the start */
636
  dbg_wb_write32(RAM_BASE + 0x0c, 0x21000001);
637
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x1c)  /* Set PC */;
638
  dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
639
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
640
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
641
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
642
  dbg_wb_write32(RAM_BASE + 0x0c, insn);  /* Set back original insn */
643
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
644
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000010, 0x4000000c, 99);
645
  result = npc + ppc + r1 + result;
646
 
647
  dbg_cpu_write32((6 << 11) + 16, 1 << 22);  /* Set step bit */
648
  for(i = 0; i < 5; i++) dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
649
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
650
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
651
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
652
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
653
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000024, 0x40000020, 101);
654
  result = npc + ppc + r1 + result;
655
 
656
  dbg_cpu_write32((0 << 11) + 16, RAM_BASE + 0x20);  /* Set PC */
657
  for(i = 0; i < 2; i++) dbg_cpu_write_reg(4, 0x00000000);  /* Unstall */
658
  dbg_cpu_read32((0 << 11) + 16, &npc);  /* Read NPC */
659
  dbg_cpu_read32((0 << 11) + 18, &ppc);  /* Read PPC */
660
  dbg_cpu_read32(0x401, &r1);  /* Read R1 */
661
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
662
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 201);
663
  result = npc + ppc + r1 + result;
664
 
665
  printf("result = %.8lx\n", result + 0x5eaddaa9);
666
#endif
667
}
668
 
669
int main(int argc,  char *argv[]) {
670
  char *redirstr;
671
  int trace_fd = 0;
672
  char *s;
673
  int err = DBG_ERR_OK;
674
 
675
  srand(getpid());
676
  if(argc != 2) {
677
    printf("JTAG protocol via parallel port for linux.\n");
678
    printf("Copyright(C) 2001 Marko Mlinar, markom@opencores.org\n\n");
679
    printf("Usage: %s JTAG port_number\n", argv[0]);
680
    return -1;
681
  }
682
 
683
#ifndef RTL_SIM
684
  if(ioperm(LPT_BASE, 3, 1)) {
685
    fprintf(stderr, "Couldn't get the port at %x\n", LPT_BASE);
686
    perror("Root privileges are required.\n");
687
    return -1;
688
  }
689
  printf("Using parallel port at %x\n", LPT_BASE);
690
#else
691
  {
692
    FILE *fin = fopen(GDB_IN, "wt+");
693
    if(fin == 0) {
694
      fprintf(stderr, "Can not open %s\n", GDB_IN);
695
      exit(1);
696
    }
697
    fclose(fin);
698
 
699
  }
700
#endif
701
#ifndef RTL_SIM
702
  /* Get rid of root privileges.  */
703
  setreuid(getuid(), getuid());
704
#endif
705
 
706
  /* Initialize a new connection to the or1k board, and make sure we are
707
     really connected.  */
708
  current_chain = -1;
709
  if ((err = dbg_reset())) goto error;
710
 
711
  /* Test the connection.  */
712
  dbg_test();
713
 
714
  /* We have a connection.  Establish server.  */
715
  argv++; argc--;
716
  printf("Dropping root privileges.\n");
717
  serverPort = strtol(*(argv),&s,10);
718
  if(*s) return -1;
719
  argv++; argc--;
720
 
721
  if(server_fd = GetServerSocket("or1ksim","tcp", serverPort)) {
722
    printf("JTAG Proxy server started on port %d\n", serverPort);
723
    printf("Press CTRL+c to exit.\n");
724
  } else {
725
    fprintf(stderr,"Cannot start JTAG Proxy server on port %d\n", serverPort);
726
    exit(-1);
727
  }
728
 
729
  /* Do endless loop of checking and handle GDB requests.  Ctrl-c exits.  */
730
  HandleServerSocket(true);
731
  return 0;
732
error:
733
  fprintf(stderr,"Connection with jtag via parallel port failed (err = %d).\n", err);
734
  exit(-1);
735
}
736
 

powered by: WebSVN 2.1.0

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