2 * This file is part of SIS.
4 * SIS, SPARC instruction simulator V2.5 Copyright (C) 1995 Jiri Gaisler,
5 * European Space Agency
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 675
19 * Mass Ave, Cambridge, MA 02139, USA.
23 /* The control space devices */
25 #include <sys/types.h>
28 #include <sys/fcntl.h>
33 #include "sim-config.h"
36 extern int32 sis_verbose
;
37 extern int32 sparclite
, sparclite_board
;
38 extern int rom8
,wrp
,uben
;
39 extern char uart_dev1
[], uart_dev2
[];
41 int dumbio
= 0; /* normal, smart, terminal oriented IO by default */
44 #define MEC_START 0x01f80000
45 #define MEC_END 0x01f80100
47 /* Memory exception waitstates */
50 /* ERC32 always adds one waitstate during RAM std */
57 /* The target's byte order is big-endian by default until we load a
58 little-endian program. */
60 int current_target_byte_order
= BIG_ENDIAN
;
62 #define MEC_WS 0 /* Waitstates per MEC access (0 ws) */
65 /* MEC register addresses */
69 #define MEC_PWDR 0x008
70 #define MEC_MEMCFG 0x010
71 #define MEC_IOCR 0x014
74 #define MEC_MAR0 0x020
75 #define MEC_MAR1 0x024
77 #define MEC_SSA1 0x020
78 #define MEC_SEA1 0x024
79 #define MEC_SSA2 0x028
80 #define MEC_SEA2 0x02C
86 #define MEC_WDOG 0x060
87 #define MEC_TRAPD 0x064
88 #define MEC_RTC_COUNTER 0x080
89 #define MEC_RTC_RELOAD 0x080
90 #define MEC_RTC_SCALER 0x084
91 #define MEC_GPT_COUNTER 0x088
92 #define MEC_GPT_RELOAD 0x088
93 #define MEC_GPT_SCALER 0x08C
94 #define MEC_TIMER_CTRL 0x098
95 #define MEC_SFSR 0x0A0
96 #define MEC_FFAR 0x0A4
97 #define MEC_ERSR 0x0B0
101 #define MEC_BRK 0x0C4
102 #define MEC_WPR 0x0C8
104 #define MEC_UARTA 0x0E0
105 #define MEC_UARTB 0x0E4
106 #define MEC_UART_CTRL 0x0E8
107 #define SIM_LOAD 0x0F0
109 /* Memory exception causes */
113 #define WATCH_EXC 0xa
114 #define BREAK_EXC 0xb
116 /* Size of UART buffers (bytes) */
119 /* Number of simulator ticks between flushing the UARTS. */
120 /* For good performance, keep above 1000 */
121 #define UART_FLUSH_TIME 3000
123 /* MEC timer control register bits */
128 #define TCR_TCRCR 0x100
129 #define TCR_TCRCL 0x200
130 #define TCR_TCRSE 0x400
131 #define TCR_TCRSL 0x800
133 /* New uart defines */
134 #define UART_TX_TIME 1000
135 #define UART_RX_TIME 1000
137 #define UARTA_SRE 0x2
138 #define UARTA_HRE 0x4
139 #define UARTA_OR 0x40
140 #define UARTA_CLR 0x80
141 #define UARTB_DR 0x10000
142 #define UARTB_SRE 0x20000
143 #define UARTB_HRE 0x40000
144 #define UARTB_OR 0x400000
145 #define UARTB_CLR 0x800000
147 #define UART_DR 0x100
148 #define UART_TSE 0x200
149 #define UART_THE 0x400
153 static char fname
[256];
154 static int32 find
= 0;
155 static uint32 mec_ssa
[2]; /* Write protection start address */
156 static uint32 mec_sea
[2]; /* Write protection end address */
157 static uint32 mec_wpr
[2]; /* Write protection control fields */
158 static uint32 mec_sfsr
;
159 static uint32 mec_ffar
;
160 static uint32 mec_ipr
;
161 static uint32 mec_imr
;
162 static uint32 mec_isr
;
163 static uint32 mec_icr
;
164 static uint32 mec_ifr
;
165 static uint32 mec_mcr
; /* MEC control register */
166 static uint32 mec_memcfg
; /* Memory control register */
167 static uint32 mec_wcr
; /* MEC waitstate register */
168 static uint32 mec_iocr
; /* MEC IO control register */
169 static uint32 posted_irq
;
170 static uint32 mec_ersr
; /* MEC error and status register */
171 static uint32 mec_tcr
; /* MEC test comtrol register */
173 static uint32 rtc_counter
;
174 static uint32 rtc_reload
;
175 static uint32 rtc_scaler
;
176 static uint32 rtc_scaler_start
;
177 static uint32 rtc_enabled
;
178 static uint32 rtc_cr
;
179 static uint32 rtc_se
;
181 static uint32 gpt_counter
;
182 static uint32 gpt_reload
;
183 static uint32 gpt_scaler
;
184 static uint32 gpt_scaler_start
;
185 static uint32 gpt_enabled
;
186 static uint32 gpt_cr
;
187 static uint32 gpt_se
;
189 static uint32 wdog_scaler
;
190 static uint32 wdog_counter
;
191 static uint32 wdog_rst_delay
;
192 static uint32 wdog_rston
;
195 init
, disabled
, enabled
, stopped
198 static enum wdog_type wdog_status
;
201 /* ROM size 1024 Kbyte */
202 #define ROM_SZ 0x100000
203 #define ROM_MASK 0x0fffff
205 /* RAM size 4 Mbyte */
206 #define RAM_START 0x02000000
207 #define RAM_END 0x02400000
208 #define RAM_MASK 0x003fffff
210 /* SPARClite boards all seem to have RAM at the same place. */
211 #define RAM_START_SLITE 0x40000000
212 #define RAM_END_SLITE 0x40400000
213 #define RAM_MASK_SLITE 0x003fffff
215 /* Memory support variables */
217 static uint32 mem_ramr_ws
; /* RAM read waitstates */
218 static uint32 mem_ramw_ws
; /* RAM write waitstates */
219 static uint32 mem_romr_ws
; /* ROM read waitstates */
220 static uint32 mem_romw_ws
; /* ROM write waitstates */
221 static uint32 mem_ramstart
; /* RAM start */
222 static uint32 mem_ramend
; /* RAM end */
223 static uint32 mem_rammask
; /* RAM address mask */
224 static uint32 mem_ramsz
; /* RAM size */
225 static uint32 mem_romsz
; /* ROM size */
226 static uint32 mem_accprot
; /* RAM write protection enabled */
227 static uint32 mem_blockprot
; /* RAM block write protection enabled */
229 static unsigned char romb
[ROM_SZ
];
230 static unsigned char ramb
[RAM_END
- RAM_START
];
233 /* UART support variables */
235 static int32 fd1
, fd2
; /* file descriptor for input file */
236 static int32 Ucontrol
; /* UART status register */
237 static unsigned char aq
[UARTBUF
], bq
[UARTBUF
];
238 static int32 anum
, aind
= 0;
239 static int32 bnum
, bind
= 0;
240 static char wbufa
[UARTBUF
], wbufb
[UARTBUF
];
241 static unsigned wnuma
;
242 static unsigned wnumb
;
243 static FILE *f1in
, *f1out
, *f2in
, *f2out
;
244 static struct termios ioc1
, ioc2
, iocold1
, iocold2
;
245 static int f1open
= 0, f2open
= 0;
247 static char uarta_sreg
, uarta_hreg
, uartb_sreg
, uartb_hreg
;
248 static uint32 uart_stat_reg
;
249 static uint32 uarta_data
, uartb_data
;
256 /* Forward declarations */
258 static void decode_ersr
PARAMS ((void));
260 static void iucomperr
PARAMS ((void));
262 static void mecparerror
PARAMS ((void));
263 static void decode_memcfg
PARAMS ((void));
264 static void decode_wcr
PARAMS ((void));
265 static void decode_mcr
PARAMS ((void));
266 static void close_port
PARAMS ((void));
267 static void mec_reset
PARAMS ((void));
268 static void mec_intack
PARAMS ((int32 level
));
269 static void chk_irq
PARAMS ((void));
270 static void mec_irq
PARAMS ((int32 level
));
271 static void set_sfsr
PARAMS ((uint32 fault
, uint32 addr
,
272 uint32 asi
, uint32 read
));
273 static int32 mec_read
PARAMS ((uint32 addr
, uint32 asi
, uint32
*data
));
274 static int mec_write
PARAMS ((uint32 addr
, uint32 data
));
275 static void port_init
PARAMS ((void));
276 static uint32 read_uart
PARAMS ((uint32 addr
));
277 static void write_uart
PARAMS ((uint32 addr
, uint32 data
));
278 static void flush_uart
PARAMS ((void));
279 static void uarta_tx
PARAMS ((void));
280 static void uartb_tx
PARAMS ((void));
281 static void uart_rx
PARAMS ((caddr_t arg
));
282 static void uart_intr
PARAMS ((caddr_t arg
));
283 static void uart_irq_start
PARAMS ((void));
284 static void wdog_intr
PARAMS ((caddr_t arg
));
285 static void wdog_start
PARAMS ((void));
286 static void rtc_intr
PARAMS ((caddr_t arg
));
287 static void rtc_start
PARAMS ((void));
288 static uint32 rtc_counter_read
PARAMS ((void));
289 static void rtc_scaler_set
PARAMS ((uint32 val
));
290 static void rtc_reload_set
PARAMS ((uint32 val
));
291 static void gpt_intr
PARAMS ((caddr_t arg
));
292 static void gpt_start
PARAMS ((void));
293 static uint32 gpt_counter_read
PARAMS ((void));
294 static void gpt_scaler_set
PARAMS ((uint32 val
));
295 static void gpt_reload_set
PARAMS ((uint32 val
));
296 static void timer_ctrl
PARAMS ((uint32 val
));
297 static unsigned char *
298 get_mem_ptr
PARAMS ((uint32 addr
, uint32 size
));
300 static void fetch_bytes
PARAMS ((int asi
, unsigned char *mem
,
301 uint32
*data
, int sz
));
303 static void store_bytes
PARAMS ((unsigned char *mem
, uint32
*data
, int sz
));
316 /* Power-on reset init */
329 if (mec_ersr
& 0x01) {
330 if (!(mec_mcr
& 0x20)) {
331 if (mec_mcr
& 0x40) {
335 printf("Error manager reset - IU in error mode\n");
340 printf("Error manager halt - IU in error mode\n");
345 if (mec_ersr
& 0x04) {
346 if (!(mec_mcr
& 0x200)) {
347 if (mec_mcr
& 0x400) {
351 printf("Error manager reset - IU comparison error\n");
356 printf("Error manager halt - IU comparison error\n");
361 if (mec_ersr
& 0x20) {
362 if (!(mec_mcr
& 0x2000)) {
363 if (mec_mcr
& 0x4000) {
367 printf("Error manager reset - MEC hardware error\n");
372 printf("Error manager halt - MEC hardware error\n");
396 /* IU error mode manager */
408 /* Check memory settings */
413 if (rom8
) mec_memcfg
&= ~0x20000;
414 else mec_memcfg
|= 0x20000;
416 mem_ramsz
= (256 * 1024) << ((mec_memcfg
>> 10) & 7);
417 mem_romsz
= (128 * 1024) << ((mec_memcfg
>> 18) & 7);
419 if (sparclite_board
) {
420 mem_ramstart
= RAM_START_SLITE
;
421 mem_ramend
= RAM_END_SLITE
;
422 mem_rammask
= RAM_MASK_SLITE
;
425 mem_ramstart
= RAM_START
;
426 mem_ramend
= RAM_END
;
427 mem_rammask
= RAM_MASK
;
430 printf("RAM start: 0x%x, RAM size: %d K, ROM size: %d K\n",
431 mem_ramstart
, mem_ramsz
>> 10, mem_romsz
>> 10);
437 mem_ramr_ws
= mec_wcr
& 3;
438 mem_ramw_ws
= (mec_wcr
>> 2) & 3;
439 mem_romr_ws
= (mec_wcr
>> 4) & 0x0f;
441 if (mem_romr_ws
> 0 ) mem_romr_ws
--;
442 mem_romr_ws
= 5 + (4*mem_romr_ws
);
444 mem_romw_ws
= (mec_wcr
>> 8) & 0x0f;
446 printf("Waitstates = RAM read: %d, RAM write: %d, ROM read: %d, ROM write: %d\n",
447 mem_ramr_ws
, mem_ramw_ws
, mem_romr_ws
, mem_romw_ws
);
453 mem_accprot
= (mec_wpr
[0] | mec_wpr
[1]);
454 mem_blockprot
= (mec_mcr
>> 3) & 1;
455 if (sis_verbose
&& mem_accprot
)
456 printf("Memory block write protection enabled\n");
457 if (mec_mcr
& 0x08000) {
461 if (sis_verbose
&& (mec_mcr
& 2))
462 printf("Software reset enabled\n");
463 if (sis_verbose
&& (mec_mcr
& 1))
464 printf("Power-down mode enabled\n");
467 /* Flush ports when simulator stops */
478 sim_stop(SIM_DESC sd
)
487 if (f1open
&& f1in
!= stdin
)
489 if (f2open
&& f2in
!= stdin
)
505 for (i
= 0; i
< 2; i
++)
506 mec_ssa
[i
] = mec_sea
[i
] = mec_wpr
[i
] = 0;
507 mec_mcr
= 0x01350014;
516 mec_memcfg
= 0x10000;
518 mec_ersr
= 0; /* MEC error and status register */
519 mec_tcr
= 0; /* MEC test comtrol register */
527 anum
= aind
= bnum
= bind
= 0;
529 uart_stat_reg
= UARTA_SRE
| UARTA_HRE
| UARTB_SRE
| UARTB_HRE
;
530 uarta_data
= uartb_data
= UART_THE
| UART_TSE
;
532 rtc_counter
= 0xffffffff;
533 rtc_reload
= 0xffffffff;
539 gpt_counter
= 0xffffffff;
540 gpt_reload
= 0xffffffff;
547 wdog_rst_delay
= 255;
548 wdog_counter
= 0xffff;
567 printf("interrupt %d acknowledged\n", level
);
568 irq_test
= mec_tcr
& 0x80000;
569 if ((irq_test
) && (mec_ifr
& (1 << level
)))
570 mec_ifr
&= ~(1 << level
);
572 mec_ipr
&= ~(1 << level
);
584 if (mec_tcr
& 0x80000) itmp
= mec_ifr
;
586 itmp
= ((mec_ipr
| itmp
) & ~mec_imr
) & 0x0fffe;
589 for (i
= 15; i
> 0; i
--) {
590 if (((itmp
>> i
) & 1) != 0) {
591 if ((sis_verbose
) && (i
> old_irl
))
592 printf("IU irl: %d\n", i
);
594 set_int(i
, mec_intack
, i
);
605 mec_ipr
|= (1 << level
);
610 set_sfsr(fault
, addr
, asi
, read
)
616 if ((asi
== 0xa) || (asi
== 0xb)) {
618 mec_sfsr
= (fault
<< 3) | (!read
<< 15);
619 mec_sfsr
|= ((mec_sfsr
& 1) ^ 1) | (mec_sfsr
& 1);
632 mec_read(addr
, asi
, data
)
638 switch (addr
& 0x0ff) {
640 case MEC_MCR
: /* 0x00 */
644 case MEC_MEMCFG
: /* 0x10 */
649 *data
= mec_iocr
; /* 0x14 */
652 case MEC_SSA1
: /* 0x20 */
653 *data
= mec_ssa
[0] | (mec_wpr
[0] << 23);
655 case MEC_SEA1
: /* 0x24 */
658 case MEC_SSA2
: /* 0x28 */
659 *data
= mec_ssa
[1] | (mec_wpr
[1] << 23);
661 case MEC_SEA2
: /* 0x2c */
665 case MEC_ISR
: /* 0x44 */
669 case MEC_IPR
: /* 0x48 */
673 case MEC_IMR
: /* 0x4c */
677 case MEC_IFR
: /* 0x54 */
681 case MEC_RTC_COUNTER
: /* 0x80 */
682 *data
= rtc_counter_read();
684 case MEC_RTC_SCALER
: /* 0x84 */
686 *data
= rtc_scaler
- (now() - rtc_scaler_start
);
691 case MEC_GPT_COUNTER
: /* 0x88 */
692 *data
= gpt_counter_read();
695 case MEC_GPT_SCALER
: /* 0x8c */
697 *data
= gpt_scaler
- (now() - gpt_scaler_start
);
703 case MEC_SFSR
: /* 0xA0 */
707 case MEC_FFAR
: /* 0xA4 */
714 strcpy(fname
, "simload");
715 find
= bfd_load(fname
);
723 case MEC_ERSR
: /* 0xB0 */
727 case MEC_TCR
: /* 0xD0 */
731 case MEC_UARTA
: /* 0xE0 */
732 case MEC_UARTB
: /* 0xE4 */
734 set_sfsr(MEC_ACC
, addr
, asi
, 1);
737 *data
= read_uart(addr
);
740 case MEC_UART_CTRL
: /* 0xE8 */
742 *data
= read_uart(addr
);
746 set_sfsr(MEC_ACC
, addr
, asi
, 1);
754 mec_write(addr
, data
)
759 printf("MEC write a: %08x, d: %08x\n",addr
,data
);
760 switch (addr
& 0x0ff) {
765 if (mec_mcr
& 0x08000) mecparerror();
773 printf(" Software reset issued\n");
779 if (mec_iocr
& 0xC0C0C0C0) mecparerror();
782 case MEC_SSA1
: /* 0x20 */
783 if (data
& 0xFE000000) mecparerror();
784 mec_ssa
[0] = data
& 0x7fffff;
785 mec_wpr
[0] = (data
>> 23) & 0x03;
786 mem_accprot
= mec_wpr
[0] || mec_wpr
[1];
787 if (sis_verbose
&& mec_wpr
[0])
788 printf("Segment 1 memory protection enabled (0x02%06x - 0x02%06x)\n",
789 mec_ssa
[0] << 2, mec_sea
[0] << 2);
791 case MEC_SEA1
: /* 0x24 */
792 if (data
& 0xFF800000) mecparerror();
793 mec_sea
[0] = data
& 0x7fffff;
795 case MEC_SSA2
: /* 0x28 */
796 if (data
& 0xFE000000) mecparerror();
797 mec_ssa
[1] = data
& 0x7fffff;
798 mec_wpr
[1] = (data
>> 23) & 0x03;
799 mem_accprot
= mec_wpr
[0] || mec_wpr
[1];
800 if (sis_verbose
&& mec_wpr
[1])
801 printf("Segment 2 memory protection enabled (0x02%06x - 0x02%06x)\n",
802 mec_ssa
[1] << 2, mec_sea
[1] << 2);
804 case MEC_SEA2
: /* 0x2c */
805 if (data
& 0xFF800000) mecparerror();
806 mec_sea
[1] = data
& 0x7fffff;
811 if (data
& 0xFFFFFF00) mecparerror();
813 if (data
& 0xFF00FF00) mecparerror();
814 write_uart(addr
, data
);
818 gpt_reload_set(data
);
822 if (data
& 0xFFFF0000) mecparerror();
823 gpt_scaler_set(data
);
827 if (data
& 0xFFFFF0F0) mecparerror();
832 rtc_reload_set(data
);
836 if (data
& 0xFFFFFF00) mecparerror();
837 rtc_scaler_set(data
);
840 case MEC_SFSR
: /* 0xA0 */
841 if (data
& 0xFFFF0880) mecparerror();
846 if (data
& 0xFFFFE000) mecparerror();
850 case MEC_IMR
: /* 0x4c */
852 if (data
& 0xFFFF8001) mecparerror();
853 mec_imr
= data
& 0x7ffe;
857 case MEC_ICR
: /* 0x50 */
859 if (data
& 0xFFFF0001) mecparerror();
860 mec_ipr
&= ~data
& 0x0fffe;
864 case MEC_IFR
: /* 0x54 */
866 if (mec_tcr
& 0x080000) {
867 if (data
& 0xFFFF0001) mecparerror();
868 mec_ifr
= data
& 0xfffe;
873 fname
[find
++] = (char) data
;
877 case MEC_MEMCFG
: /* 0x10 */
878 if (data
& 0xC0E08000) mecparerror();
881 if (mec_memcfg
& 0xc0e08000)
885 case MEC_WCR
: /* 0x18 */
890 case MEC_ERSR
: /* 0xB0 */
891 if (mec_tcr
& 0x100000)
892 if (data
& 0xFFFFEFC0) mecparerror();
893 mec_ersr
= data
& 0x103f;
896 case MEC_TCR
: /* 0xD0 */
897 if (data
& 0xFFE1FFC0) mecparerror();
898 mec_tcr
= data
& 0x1e003f;
901 case MEC_WDOG
: /* 0x60 */
902 wdog_scaler
= (data
>> 16) & 0x0ff;
903 wdog_counter
= data
& 0x0ffff;
904 wdog_rst_delay
= data
>> 24;
906 if (wdog_status
== stopped
)
908 wdog_status
= enabled
;
911 case MEC_TRAPD
: /* 0x64 */
912 if (wdog_status
== init
) {
913 wdog_status
= disabled
;
915 printf("Watchdog disabled\n");
925 set_sfsr(MEC_ACC
, addr
, 0xb, 0);
935 static int ifd1
= -1, ifd2
= -1, ofd1
= -1, ofd2
= -1;
941 return; /* do nothing */
943 tcsetattr(0, TCSANOW
, &ioc1
);
945 tcsetattr(0, TCSANOW
, &ioc2
);
952 return; /* do nothing */
954 tcsetattr(0, TCSANOW
, &iocold1
);
956 tcsetattr(0, TCSANOW
, &iocold2
);
959 #define DO_STDIO_READ( _fd_, _buf_, _len_ ) \
961 ? (0) /* no bytes read, no delay */ \
962 : read( _fd_, _buf_, _len_ ) )
980 if (uart_dev1
[0] != 0)
981 if ((fd1
= open(uart_dev1
, O_RDWR
| O_NONBLOCK
)) < 0) {
982 printf("Warning, couldn't open output device %s\n", uart_dev1
);
985 printf("serial port A on %s\n", uart_dev1
);
986 f1in
= f1out
= fdopen(fd1
, "r+");
990 if (f1in
) ifd1
= fileno(f1in
);
993 printf("serial port A on stdin/stdout\n");
995 tcgetattr(ifd1
, &ioc1
);
997 ioc1
.c_lflag
&= ~(ICANON
| ECHO
);
999 ioc1
.c_cc
[VTIME
] = 0;
1005 ofd1
= fileno(f1out
);
1006 if (!dumbio
&& ofd1
== 1) setbuf(f1out
, NULL
);
1009 if (uart_dev2
[0] != 0)
1010 if ((fd2
= open(uart_dev2
, O_RDWR
| O_NONBLOCK
)) < 0) {
1011 printf("Warning, couldn't open output device %s\n", uart_dev2
);
1014 printf("serial port B on %s\n", uart_dev2
);
1015 f2in
= f2out
= fdopen(fd2
, "r+");
1016 setbuf(f2out
, NULL
);
1019 if (f2in
) ifd2
= fileno(f2in
);
1022 printf("serial port B on stdin/stdout\n");
1024 tcgetattr(ifd2
, &ioc2
);
1026 ioc2
.c_lflag
&= ~(ICANON
| ECHO
);
1027 ioc2
.c_cc
[VMIN
] = 0;
1028 ioc2
.c_cc
[VTIME
] = 0;
1034 ofd2
= fileno(f2out
);
1035 if (!dumbio
&& ofd2
== 1) setbuf(f2out
, NULL
);
1050 switch (addr
& 0xff) {
1052 case 0xE0: /* UART 1 */
1057 if ((aind
+ 1) < anum
)
1059 return (0x700 | (uint32
) aq
[aind
++]);
1062 anum
= DO_STDIO_READ(ifd1
, aq
, UARTBUF
);
1066 if ((aind
+ 1) < anum
)
1068 return (0x700 | (uint32
) aq
[aind
++]);
1070 return (0x600 | (uint32
) aq
[aind
]);
1076 uarta_data
&= ~UART_DR
;
1077 uart_stat_reg
&= ~UARTA_DR
;
1085 case 0xE4: /* UART 2 */
1089 if ((bind
+ 1) < bnum
)
1091 return (0x700 | (uint32
) bq
[bind
++]);
1094 bnum
= DO_STDIO_READ(ifd2
, bq
, UARTBUF
);
1098 if ((bind
+ 1) < bnum
)
1100 return (0x700 | (uint32
) bq
[bind
++]);
1102 return (0x600 | (uint32
) bq
[bind
]);
1108 uartb_data
&= ~UART_DR
;
1109 uart_stat_reg
&= ~UARTB_DR
;
1117 case 0xE8: /* UART status register */
1123 Ucontrol
|= 0x00000001;
1126 anum
= DO_STDIO_READ(ifd1
, aq
, UARTBUF
);
1129 Ucontrol
|= 0x00000001;
1135 Ucontrol
|= 0x00010000;
1138 bnum
= DO_STDIO_READ(ifd2
, bq
, UARTBUF
);
1141 Ucontrol
|= 0x00010000;
1147 Ucontrol
|= 0x00060006;
1150 return (uart_stat_reg
);
1158 printf("Read from unimplemented MEC register (%x)\n", addr
);
1165 write_uart(addr
, data
)
1171 c
= (unsigned char) data
;
1172 switch (addr
& 0xff) {
1174 case 0xE0: /* UART A */
1177 if (wnuma
< UARTBUF
)
1181 wnuma
-= fwrite(wbufa
, 1, wnuma
, f1out
);
1187 if (uart_stat_reg
& UARTA_SRE
) {
1189 uart_stat_reg
&= ~UARTA_SRE
;
1190 event(uarta_tx
, 0, UART_TX_TIME
);
1193 uart_stat_reg
&= ~UARTA_HRE
;
1198 case 0xE4: /* UART B */
1201 if (wnumb
< UARTBUF
)
1205 wnumb
-= fwrite(wbufb
, 1, wnumb
, f2out
);
1211 if (uart_stat_reg
& UARTB_SRE
) {
1213 uart_stat_reg
&= ~UARTB_SRE
;
1214 event(uartb_tx
, 0, UART_TX_TIME
);
1217 uart_stat_reg
&= ~UARTB_HRE
;
1221 case 0xE8: /* UART status register */
1223 if (data
& UARTA_CLR
) {
1224 uart_stat_reg
&= 0xFFFF0000;
1225 uart_stat_reg
|= UARTA_SRE
| UARTA_HRE
;
1227 if (data
& UARTB_CLR
) {
1228 uart_stat_reg
&= 0x0000FFFF;
1229 uart_stat_reg
|= UARTB_SRE
| UARTB_HRE
;
1235 printf("Write to unimplemented MEC register (%x)\n", addr
);
1243 while (wnuma
&& f1open
)
1244 wnuma
-= fwrite(wbufa
, 1, wnuma
, f1out
);
1245 while (wnumb
&& f2open
)
1246 wnumb
-= fwrite(wbufb
, 1, wnumb
, f2out
);
1255 while (f1open
&& fwrite(&uarta_sreg
, 1, 1, f1out
) != 1);
1256 if (uart_stat_reg
& UARTA_HRE
) {
1257 uart_stat_reg
|= UARTA_SRE
;
1259 uarta_sreg
= uarta_hreg
;
1260 uart_stat_reg
|= UARTA_HRE
;
1261 event(uarta_tx
, 0, UART_TX_TIME
);
1269 while (f2open
&& fwrite(&uartb_sreg
, 1, 1, f2out
) != 1);
1270 if (uart_stat_reg
& UARTB_HRE
) {
1271 uart_stat_reg
|= UARTB_SRE
;
1273 uartb_sreg
= uartb_hreg
;
1274 uart_stat_reg
|= UARTB_HRE
;
1275 event(uartb_tx
, 0, UART_TX_TIME
);
1290 rsize
= DO_STDIO_READ(ifd1
, &rxd
, 1);
1292 uarta_data
= UART_DR
| rxd
;
1293 if (uart_stat_reg
& UARTA_HRE
)
1294 uarta_data
|= UART_THE
;
1295 if (uart_stat_reg
& UARTA_SRE
)
1296 uarta_data
|= UART_TSE
;
1297 if (uart_stat_reg
& UARTA_DR
) {
1298 uart_stat_reg
|= UARTA_OR
;
1299 mec_irq(7); /* UART error interrupt */
1301 uart_stat_reg
|= UARTA_DR
;
1306 rsize
= DO_STDIO_READ(ifd2
, &rxd
, 1);
1308 uartb_data
= UART_DR
| rxd
;
1309 if (uart_stat_reg
& UARTB_HRE
)
1310 uartb_data
|= UART_THE
;
1311 if (uart_stat_reg
& UARTB_SRE
)
1312 uartb_data
|= UART_TSE
;
1313 if (uart_stat_reg
& UARTB_DR
) {
1314 uart_stat_reg
|= UARTB_OR
;
1315 mec_irq(7); /* UART error interrupt */
1317 uart_stat_reg
|= UARTB_DR
;
1320 event(uart_rx
, 0, UART_RX_TIME
);
1327 read_uart(0xE8); /* Check for UART interrupts every 1000 clk */
1328 flush_uart(); /* Flush UART ports */
1329 event(uart_intr
, 0, UART_FLUSH_TIME
);
1337 event(uart_intr
, 0, UART_FLUSH_TIME
);
1340 event(uart_rx
, 0, UART_RX_TIME
);
1351 if (wdog_status
== disabled
) {
1352 wdog_status
= stopped
;
1357 event(wdog_intr
, 0, wdog_scaler
+ 1);
1360 printf("Watchdog reset!\n");
1366 wdog_counter
= wdog_rst_delay
;
1367 event(wdog_intr
, 0, wdog_scaler
+ 1);
1376 event(wdog_intr
, 0, wdog_scaler
+ 1);
1378 printf("Watchdog started, scaler = %d, counter = %d\n",
1379 wdog_scaler
, wdog_counter
);
1390 if (rtc_counter
== 0) {
1394 rtc_counter
= rtc_reload
;
1400 event(rtc_intr
, 0, rtc_scaler
+ 1);
1401 rtc_scaler_start
= now();
1405 printf("RTC stopped\n\r");
1414 printf("RTC started (period %d)\n\r", rtc_scaler
+ 1);
1415 event(rtc_intr
, 0, rtc_scaler
+ 1);
1416 rtc_scaler_start
= now();
1423 return (rtc_counter
);
1430 rtc_scaler
= val
& 0x0ff; /* eight-bit scaler only */
1444 if (gpt_counter
== 0) {
1447 gpt_counter
= gpt_reload
;
1453 event(gpt_intr
, 0, gpt_scaler
+ 1);
1454 gpt_scaler_start
= now();
1458 printf("GPT stopped\n\r");
1467 printf("GPT started (period %d)\n\r", gpt_scaler
+ 1);
1468 event(gpt_intr
, 0, gpt_scaler
+ 1);
1469 gpt_scaler_start
= now();
1476 return (gpt_counter
);
1483 gpt_scaler
= val
& 0x0ffff; /* 16-bit scaler */
1498 rtc_cr
= ((val
& TCR_TCRCR
) != 0);
1499 if (val
& TCR_TCRCL
) {
1500 rtc_counter
= rtc_reload
;
1502 if (val
& TCR_TCRSL
) {
1504 rtc_se
= ((val
& TCR_TCRSE
) != 0);
1505 if (rtc_se
&& (rtc_enabled
== 0))
1508 gpt_cr
= (val
& TCR_GACR
);
1509 if (val
& TCR_GACL
) {
1510 gpt_counter
= gpt_reload
;
1512 if (val
& TCR_GACL
) {
1514 gpt_se
= (val
& TCR_GASE
) >> 2;
1515 if (gpt_se
&& (gpt_enabled
== 0))
1520 /* Retrieve data from target memory. MEM points to location from which
1521 to read the data; DATA points to words where retrieved data will be
1522 stored in host byte order. SZ contains log(2) of the number of bytes
1523 to retrieve, and can be 0 (1 byte), 1 (one half-word), 2 (one word),
1524 or 3 (two words). */
1527 fetch_bytes (asi
, mem
, data
, sz
)
1533 if (CURRENT_TARGET_BYTE_ORDER
== BIG_ENDIAN
1534 || asi
== 8 || asi
== 9) {
1537 data
[1] = (((uint32
) mem
[7]) & 0xff) |
1538 ((((uint32
) mem
[6]) & 0xff) << 8) |
1539 ((((uint32
) mem
[5]) & 0xff) << 16) |
1540 ((((uint32
) mem
[4]) & 0xff) << 24);
1541 /* Fall through to 2 */
1543 data
[0] = (((uint32
) mem
[3]) & 0xff) |
1544 ((((uint32
) mem
[2]) & 0xff) << 8) |
1545 ((((uint32
) mem
[1]) & 0xff) << 16) |
1546 ((((uint32
) mem
[0]) & 0xff) << 24);
1549 data
[0] = (((uint32
) mem
[1]) & 0xff) |
1550 ((((uint32
) mem
[0]) & 0xff) << 8);
1553 data
[0] = mem
[0] & 0xff;
1560 data
[1] = ((((uint32
) mem
[7]) & 0xff) << 24) |
1561 ((((uint32
) mem
[6]) & 0xff) << 16) |
1562 ((((uint32
) mem
[5]) & 0xff) << 8) |
1563 (((uint32
) mem
[4]) & 0xff);
1564 /* Fall through to 4 */
1566 data
[0] = ((((uint32
) mem
[3]) & 0xff) << 24) |
1567 ((((uint32
) mem
[2]) & 0xff) << 16) |
1568 ((((uint32
) mem
[1]) & 0xff) << 8) |
1569 (((uint32
) mem
[0]) & 0xff);
1572 data
[0] = ((((uint32
) mem
[1]) & 0xff) << 8) |
1573 (((uint32
) mem
[0]) & 0xff);
1576 data
[0] = mem
[0] & 0xff;
1583 /* Store data in target byte order. MEM points to location to store data;
1584 DATA points to words in host byte order to be stored. SZ contains log(2)
1585 of the number of bytes to retrieve, and can be 0 (1 byte), 1 (one half-word),
1586 2 (one word), or 3 (two words). */
1589 store_bytes (mem
, data
, sz
)
1594 if (CURRENT_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
) {
1597 mem
[7] = (data
[1] >> 24) & 0xff;
1598 mem
[6] = (data
[1] >> 16) & 0xff;
1599 mem
[5] = (data
[1] >> 8) & 0xff;
1600 mem
[4] = data
[1] & 0xff;
1601 /* Fall through to 2 */
1603 mem
[3] = (data
[0] >> 24) & 0xff;
1604 mem
[2] = (data
[0] >> 16) & 0xff;
1605 /* Fall through to 1 */
1607 mem
[1] = (data
[0] >> 8) & 0xff;
1608 /* Fall through to 0 */
1610 mem
[0] = data
[0] & 0xff;
1616 mem
[7] = data
[1] & 0xff;
1617 mem
[6] = (data
[1] >> 8) & 0xff;
1618 mem
[5] = (data
[1] >> 16) & 0xff;
1619 mem
[4] = (data
[1] >> 24) & 0xff;
1620 /* Fall through to 2 */
1622 mem
[3] = data
[0] & 0xff;
1623 mem
[2] = (data
[0] >> 8) & 0xff;
1624 mem
[1] = (data
[0] >> 16) & 0xff;
1625 mem
[0] = (data
[0] >> 24) & 0xff;
1628 mem
[1] = data
[0] & 0xff;
1629 mem
[0] = (data
[0] >> 8) & 0xff;
1632 mem
[0] = data
[0] & 0xff;
1640 /* Memory emulation */
1643 memory_read(asi
, addr
, data
, sz
, ws
)
1655 printf("Inserted MEC error %d\n",errmec
);
1656 set_sfsr(errmec
, addr
, asi
, 1);
1657 if (errmec
== 5) mecparerror();
1658 if (errmec
== 6) iucomperr();
1664 if ((addr
>= mem_ramstart
) && (addr
< (mem_ramstart
+ mem_ramsz
))) {
1665 fetch_bytes (asi
, &ramb
[addr
& mem_rammask
], data
, sz
);
1668 } else if ((addr
>= MEC_START
) && (addr
< MEC_END
)) {
1669 mexc
= mec_read(addr
, asi
, data
);
1671 set_sfsr(MEC_ACC
, addr
, asi
, 1);
1681 if ((addr
< 0x100000) ||
1682 ((addr
>= 0x80000000) && (addr
< 0x80100000))) {
1683 fetch_bytes (asi
, &romb
[addr
& ROM_MASK
], data
, sz
);
1686 } else if ((addr
>= 0x10000000) &&
1687 (addr
< (0x10000000 + (512 << (mec_iocr
& 0x0f)))) &&
1688 (mec_iocr
& 0x10)) {
1693 } else if (addr
< mem_romsz
) {
1694 fetch_bytes (asi
, &romb
[addr
], data
, sz
);
1699 } else if (addr
< mem_romsz
) {
1700 fetch_bytes (asi
, &romb
[addr
], data
, sz
);
1707 printf("Memory exception at %x (illegal address)\n", addr
);
1708 set_sfsr(UIMP_ACC
, addr
, asi
, 1);
1714 memory_write(asi
, addr
, data
, sz
, ws
)
1732 printf("Inserted MEC error %d\n",errmec
);
1733 set_sfsr(errmec
, addr
, asi
, 0);
1734 if (errmec
== 5) mecparerror();
1735 if (errmec
== 6) iucomperr();
1741 if ((addr
>= mem_ramstart
) && (addr
< (mem_ramstart
+ mem_ramsz
))) {
1744 waddr
= (addr
& 0x7fffff) >> 2;
1745 for (i
= 0; i
< 2; i
++)
1747 (((asi
== 0xa) && (mec_wpr
[i
] & 1)) ||
1748 ((asi
== 0xb) && (mec_wpr
[i
] & 2))) &&
1749 ((waddr
>= mec_ssa
[i
]) && ((waddr
| (sz
== 3)) < mec_sea
[i
]));
1751 if (((mem_blockprot
) && (wphit
[0] || wphit
[1])) ||
1752 ((!mem_blockprot
) &&
1753 !((mec_wpr
[0] && wphit
[0]) || (mec_wpr
[1] && wphit
[1]))
1756 printf("Memory access protection error at 0x%08x\n", addr
);
1757 set_sfsr(PROT_EXC
, addr
, asi
, 0);
1763 store_bytes (&ramb
[addr
& mem_rammask
], data
, sz
);
1768 *ws
= mem_ramw_ws
+ 3;
1774 *ws
= 2 * mem_ramw_ws
+ STD_WS
;
1778 } else if ((addr
>= MEC_START
) && (addr
< MEC_END
)) {
1779 if ((sz
!= 2) || (asi
!= 0xb)) {
1780 set_sfsr(MEC_ACC
, addr
, asi
, 0);
1784 mexc
= mec_write(addr
, *data
);
1786 set_sfsr(MEC_ACC
, addr
, asi
, 0);
1797 ((addr
< 0x100000) || ((addr
>= 0x80000000) && (addr
< 0x80100000)))) {
1799 *ws
= sz
== 3 ? 8 : 4;
1800 store_bytes (&romb
[addr
], data
, sz
);
1802 } else if ((addr
>= 0x10000000) &&
1803 (addr
< (0x10000000 + (512 << (mec_iocr
& 0x0f)))) &&
1804 (mec_iocr
& 0x10)) {
1805 erareg
= *data
& 0x0e;
1809 } else if ((addr
< mem_romsz
) && (mec_memcfg
& 0x10000) && (wrp
) &&
1810 (((mec_memcfg
& 0x20000) && (sz
> 1)) ||
1811 (!(mec_memcfg
& 0x20000) && (sz
== 0)))) {
1813 *ws
= mem_romw_ws
+ 1;
1815 *ws
+= mem_romw_ws
+ STD_WS
;
1816 store_bytes (&romb
[addr
], data
, sz
);
1820 } else if ((addr
< mem_romsz
) && (mec_memcfg
& 0x10000) && (wrp
) &&
1821 (((mec_memcfg
& 0x20000) && (sz
> 1)) ||
1822 (!(mec_memcfg
& 0x20000) && (sz
== 0)))) {
1824 *ws
= mem_romw_ws
+ 1;
1826 *ws
+= mem_romw_ws
+ STD_WS
;
1827 store_bytes (&romb
[addr
], data
, sz
);
1835 set_sfsr(UIMP_ACC
, addr
, asi
, 0);
1839 static unsigned char *
1840 get_mem_ptr(addr
, size
)
1844 if ((addr
+ size
) < ROM_SZ
) {
1845 return (&romb
[addr
]);
1846 } else if ((addr
>= mem_ramstart
) && ((addr
+ size
) < mem_ramend
)) {
1847 return (&ramb
[addr
& mem_rammask
]);
1851 else if ((era
) && ((addr
<0x100000) ||
1852 ((addr
>= (unsigned) 0x80000000) && ((addr
+ size
) < (unsigned) 0x80100000)))) {
1853 return (&romb
[addr
& ROM_MASK
]);
1857 return ((char *) -1);
1861 sis_memory_write(addr
, data
, length
)
1868 if ((mem
= get_mem_ptr(addr
, length
)) == ((char *) -1))
1871 memcpy(mem
, data
, length
);
1876 sis_memory_read(addr
, data
, length
)
1883 if ((mem
= get_mem_ptr(addr
, length
)) == ((char *) -1))
1886 memcpy(data
, mem
, length
);