well, should imho work, i bet it wont though!
[my-code/fpga.git] / jtag / parallel.c
1 /*
2  * doing jtag via the
3  *
4  * 'low cost jtag parallel programming cable'
5  *
6  * author: hackbard@hackdaworld.org
7  *
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/io.h>
14
15 #define PARPORT 0x378
16
17 #define TMS     (1<<2)  /* d2 */
18 #define TDI     (1<<0) /* d0 */
19 #define TCK     (1<<1) /* d1 */
20 #define TDO     (1<<4) /* select in */
21
22 #define JTAG_INITIALIZED        0x01
23
24 #define FALSE 0
25 #define TRUE 1
26
27 typedef unsigned char u8;
28 typedef unsigned short int u16;
29 typedef unsigned int u32;
30
31 typedef struct s_jtag {
32         u16 port;
33         u8 status;
34 } t_jtag;
35
36 int jtag_set_port(t_jtag *jtag,u16 port) {
37
38         jtag->port=port;
39
40         return 0;
41 }
42
43 int jtag_init(t_jtag *jtag) {
44
45         int ret;
46         u8 out;
47
48         out=0;
49
50         /* permissions */
51         ret=ioperm(jtag->port,2,TRUE);
52         if(ret<0) {
53                 perror("ioperm");
54                 return -1;
55         }
56
57         /* set tck to zero */
58         outb(out,jtag->port);
59
60         return 0;
61 }
62
63 u8 jtag_act(t_jtag *jtag,u8 tms,u8 tdi) {
64
65         u8 out;
66         u8 tdo;
67         int i;
68
69         out=0;
70
71         if(tms) out|=(1<<TMS);
72         if(tdi) out|=(1<<TDI);
73
74         /* with tck high */
75         outb((out|TCK),jtag->port);
76
77         /* with tck low */
78         outb(out,jtag->port);
79
80         tdo=inb(jtag->port+1);
81 printf("%0x2 %c | ",tdo,((tdo<0x20)|(tdo>0x7e))?'.':tdo);
82 for(i=0;i<8;i++) printf("%d ",((tdo<<(7-i))&0x01)?1:0);
83 printf("\n");
84
85         return tdo;
86 }
87
88 int jtag_reset(t_jtag *jtag) {
89
90         int i;
91
92         for(i=0;i<5;i++) jtag_act(jtag,1,0);
93
94         return 0;
95 }
96
97 int jtag_shift_dr(t_jtag *jtag) {
98
99         /* assume 'test logic reset' status */
100
101         jtag_act(jtag,0,0);
102         jtag_act(jtag,1,0);
103         jtag_act(jtag,0,0);
104         jtag_act(jtag,0,0);
105
106         return 0;
107 }
108
109 int main(int argc,char **argv) {
110
111         t_jtag jtag;
112         int i;
113
114         jtag_init(&jtag);
115
116         jtag_reset(&jtag);
117
118         jtag_shift_dr(&jtag);
119
120         for(i=0;i<32;i++) jtag_act(&jtag,0,0);
121
122         return 0;
123 }