2 * Simulator of microcontrollers (fpga.cc)
4 * Copyright (C) 2020 Drotos Daniel
6 * To contact author send email to dr.dkdb@gmail.com
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
37 -------------------------------------------------------------------------
40 cl_led::cl_led(class cl_fpga
*the_fpga
, int ax
, int ay
, u32_t amask
):
51 cl_led::refresh(bool force
)
53 class cl_hw_io
*io
= fpga
->get_io();
54 u32_t a
= fpga
->pb
->get() & mask
;
56 if (force
|| (a
!= l
))
61 io
->dd_cprintf("led_on", "@");
66 io
->dd_cprintf("led_off", ".");
75 -------------------------------------------------------------------------
78 cl_rgb::cl_rgb(class cl_fpga
*the_fpga
, int ax
, int ay
, u32_t amask
):
79 cl_led(the_fpga
, ax
, ay
, amask
)
85 cl_rgb::refresh(bool force
)
87 u32_t gm
= mask
, rm
= mask
<<8, bm
= mask
<<16;
89 class cl_hw_io
*io
= fpga
->get_io();
90 u32_t a
= fpga
->pb
->get() & m
;
92 if (force
|| (a
!= l
))
102 io
->dd_printf("\033[1m");
105 io
->dd_color("answer");
106 io
->dd_printf("%c", a
?'@':'.');
109 if (a
) io
->dd_color("answer");
116 -------------------------------------------------------------------------
119 cl_seg::cl_seg(class cl_fpga
*the_fpga
, int ax
, int ay
, int adigit
):
120 cl_led(the_fpga
, ax
, ay
, 0)
125 static const char *dsp
[3]= {
127 //1234567890123456789012345678901234567890123456789
128 " _ _ _ _ _ _ _ _ _ _ _ _ ",
129 "| | | _| _| |_| |_ |_ | |_| |_| |_| |_ | _| |_ |_ ",
130 "|_| | |_ _| | _| |_| | |_| _| | | |_| |_ |_| |_ | "
134 cl_seg::refresh(bool force
)
136 class cl_hw_io
*io
= fpga
->get_io();
137 u32_t sw
= fpga
->pj
->read(), act
, act_what
;
138 u32_t l
, mask
, a
, lw
;
139 class cl_p1516
*uc
= (class cl_p1516
*)(fpga
->uc
);
142 int c
= -digit
+ fpga
->d2c_b
;
143 u32_t b_c
= fpga
->bc
->read(), ctr
;
145 if (last_ctrl
!= ctr
)
148 switch ((ctr
>>4)&0xf)
150 case 0: act
= fpga
->pa
->get(); w
="PA= "; break;
151 case 1: act
= fpga
->pb
->get(); w
="PB= "; break;
152 case 2: act
= fpga
->pc
->get(); w
="PC= "; break;
153 case 3: act
= fpga
->pd
->get(); w
="PD= "; break;
157 w
.format("R%d=", sw
&0xf);
159 w
.format("R%d= ", sw
&0xf);
164 //act= (digit<=3)?(fpga->pd->get()):(fpga->pc->get());
165 a
= 0xff02; // ODR of PC
167 act
= fpga
->uc
->rom
->read(a
);
175 if (force
|| (act_what
!= last_what
))
178 io
->dd_cprintf("ui_label", "%s", w
.c_str());
182 mask
= 0xf << (digit
*4);
185 if (force
|| (act
!= l
) || (act_what
!= last_what
))
190 io
->tu_fgcolor(1); io
->dd_printf("\033[1m");
192 io
->dd_printf("%c%c%c", dsp
[0][s
], dsp
[0][s
+1], dsp
[0][s
+2]);
194 io
->dd_printf("%c%c%c", dsp
[1][s
], dsp
[1][s
+1], dsp
[1][s
+2]);
196 io
->dd_printf("%c%c%c", dsp
[2][s
], dsp
[2][s
+1], dsp
[2][s
+2]);
197 io
->dd_printf("\033[0m");
198 io
->dd_color("answer");
199 last
= (last
& ~mask
) | act
;
209 case 3: mask
= 0xff000000; sby
= 3*8; break;
210 case 2: mask
= 0x00ff0000; sby
= 2*8; break;
211 case 1: mask
= 0x0000ff00; sby
= 1*8; break;
212 default: mask
= 0x000000ff; sby
= 0*8; break;
216 if (force
|| (act
!= l
) || (act_what
!= last_what
))
218 int bits
= (act
>> sby
) & 0xff;
219 io
->tu_fgcolor(1); io
->dd_printf("\033[1m");
221 io
->dd_printf(" %c ", (bits
&1)?'_':' ');
223 io
->dd_printf("%c%c%c", (bits
&0x20)?'|':' ', (bits
&0x40)?'_':' ', (bits
&2)?'|':' ');
225 io
->dd_printf("%c%c%c", (bits
&0x10)?'|':' ', (bits
&8)?'_':' ', (bits
&4)?'|':' ');
226 io
->dd_printf("\033[0m");
227 io
->dd_color("answer");
228 last
= (last
& ~mask
) | act
;
237 class cl_hw_io
*io
= fpga
->get_io();
239 io
->dd_cprintf("ui_label", "%d", digit
);
245 -------------------------------------------------------------------------
248 cl_ibit::cl_ibit(class cl_fpga
*the_fpga
, int ax
, int ay
, int amask
, char akey
):
261 -------------------------------------------------------------------------
264 cl_sw::cl_sw(class cl_fpga
*the_fpga
, int ax
, int ay
, int amask
, char akey
):
265 cl_ibit(the_fpga
, ax
, ay
, amask
, akey
)
270 cl_sw::refresh(bool force
)
273 u32_t v
= 0, act
, a
, l
;
274 class cl_hw_io
*io
= fpga
->get_io();
276 class cl_memory_cell
*p
= fpga
->pjp
;
291 if (force
|| (a
!= l
))
293 const char *d0
= "_ #";
294 const char *d1
= "_#_";
295 const char *p
= v
?d1
:d0
;
296 if (!p
) io
->dd_color("answer");
297 else io
->dd_color(v
?"sw_on":"sw_off");
298 io
->tu_go(x
,y
+0); io
->dd_printf("%c", p
[0]);
299 io
->tu_go(x
,y
+1); io
->dd_printf("%c", p
[1]);
300 io
->tu_go(x
,y
+2); io
->dd_printf("%c", p
[2]);
301 io
->dd_color("answer");
309 class cl_hw_io
*io
= fpga
->get_io();
312 io
->dd_cprintf("ui_mkey", "%c", key
);
316 cl_sw::handle_input(int c
)
318 cl_memory_cell
*p
= fpga
->pjp
;
319 if ((c
== key
) || ((key
=='y')&&(c
=='z')))
335 -------------------------------------------------------------------------
338 cl_btn::cl_btn(class cl_fpga
*the_fpga
, int ax
, int ay
, int amask
, char akey
):
339 cl_ibit(the_fpga
, ax
, ay
, amask
, akey
)
344 cl_btn::refresh(bool force
)
347 u32_t act
, a
, l
, v
= 0;
348 class cl_hw_io
*io
= fpga
->get_io();
350 class cl_memory_cell
*p
= fpga
->pip
;
365 if (force
|| (a
!= l
))
367 if (!p
) io
->dd_color("answer");
368 else io
->dd_color(v
?"btn_on":"btn_off");
370 io
->dd_printf("_%c_", c
);
371 io
->dd_color("answer");
379 class cl_hw_io
*io
= fpga
->get_io();
382 io
->dd_cprintf("ui_mkey", "%c", key
);
386 cl_btn::handle_input(int c
)
388 cl_memory_cell
*p
= fpga
->pip
;
393 class cl_p1516
*u
= (class cl_p1516
*)(fpga
->uc
);
398 u
->btn_edge(0, v
&mask
);
400 u
->btn_edge(1, v
&mask
);
410 -------------------------------------------------------------------------
413 cl_fpga::cl_fpga(class cl_uc
*auc
, int aid
, chars aid_string
):
414 cl_hw(auc
, HW_DUMMY
, aid
, aid_string
)
417 for (i
= 0; i
<16; i
++)
421 for (i
= 0; i
<16; i
++)
425 pa
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff00);
426 pb
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff01);
427 pc
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff02);
428 pd
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff03);
429 pi
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff20);
430 pj
= (class cl_cell32
*)register_cell(uc
->rom
, 0xff10);
431 bc
= (class cl_cell32
*)register_cell(uc
->rom
, 0xfff0);
432 if ((uc
->symbol2cell((char*)"pi_pins", &pip
)))
436 if ((uc
->symbol2cell((char*)"pj_pins", &pjp
)))
440 basey
= 13; // row of leds
462 io
= new cl_hw_io(this);
464 application
->get_commander()->add_console(io
);
470 cl_fpga::new_io(class cl_f
*f_in
, class cl_f
*f_out
)
472 cl_hw::new_io(f_in
, f_out
);
474 io
->dd_printf("\033[2 q");
476 f_in
->set_escape(true);
481 cl_fpga::handle_input(int c
)
487 if (btns
[i
]->handle_input(c
))
491 if (sws
[i
]->handle_input(c
))
497 //printf("c=%8x\n",c);
500 case 'A': sw
&= ~0xf0; sw
|= 0x00; pjp
->W(sw
); break;
501 case 'B': sw
&= ~0xf0; sw
|= 0x10; pjp
->W(sw
); break;
502 case 'C': sw
&= ~0xf0; sw
|= 0x20; pjp
->W(sw
); break;
503 case 'D': sw
&= ~0xf0; sw
|= 0x30; pjp
->W(sw
); break;
504 case 'R': sw
&= ~0xf0; sw
|= 0x90; pjp
->W(sw
); break;
505 case 'S': sw
&= ~0xf0; sw
|= 0xa0; pjp
->W(sw
); break;
506 case TU_UP
: sw
&= ~0xf; sw
|= ((rx
+1)&0xf); pjp
->W(sw
); break;
507 case TU_DOWN
: sw
&= ~0xf; sw
|= ((rx
-1)&0xf); pjp
->W(sw
); break;
511 ret
= cl_hw::handle_input(c
); // handle default keys
517 cl_fpga::refresh_leds(bool force
)
524 leds
[i
]->refresh(force
);
530 cl_fpga::refresh_segs(bool force
)
537 segs
[i
]->refresh(force
);
543 cl_fpga::refresh_sws(bool force
)
550 sws
[i
]->refresh(force
);
556 cl_fpga::refresh_btns(bool force
)
563 btns
[i
]->refresh(force
);
569 cl_fpga::refresh_display(bool force
)
583 cl_fpga::draw_display(void)
588 io
->dd_color("led_on");
590 cl_hw::draw_display();
591 draw_fpga(); // board specific
604 io
->tu_go(2+8*5-9-7+3-16-6,basey
-7);
605 io
->dd_cprintf("ui_mkey" , "[S]");
606 io
->dd_cprintf("ui_mitem", "eg ");
607 io
->dd_cprintf("ui_mkey" , "[ABCD] ");
608 io
->dd_cprintf("ui_mitem", "PX ");
609 io
->dd_cprintf("ui_mkey" , "[R] ");
610 io
->dd_cprintf("ui_mitem", "RX ");
611 io
->dd_cprintf("ui_mkey" , "[up] ");
612 io
->dd_cprintf("ui_mitem", "R+ ");
613 io
->dd_cprintf("ui_mkey" , "[dn] ");
614 io
->dd_cprintf("ui_mitem", "R- ");
615 refresh_display(true);
620 cl_fpga::read(class cl_memory_cell
*cell
)
630 cl_fpga::write(class cl_memory_cell
*cell
, t_mem
*val
)
649 -------------------------------------------------------------------------
652 cl_n4::cl_n4(class cl_uc
*auc
, int aid
, chars aid_string
):
653 cl_fpga(auc
, aid
, aid_string
)
663 for (i
=0, m
=1; i
<16; i
++, m
<<=1)
664 leds
[i
]= new cl_led(this, 2+16*3-i
*3,basey
, m
);
671 for (i
=0, d
=0; i
<8; i
++, d
++)
672 segs
[i
]= new cl_seg(this, 2+8*5-i
*5,basey
-6, d
);
680 btns
[0]= new cl_btn(this, 2+16*3+5+5,basey
-5, 1, '0');
682 btns
[1]= new cl_btn(this, 2+16*3+5+5,basey
-2, 2, '1');
684 btns
[2]= new cl_btn(this, 2+16*3+5+5,basey
-8, 4, '2');
686 btns
[3]= new cl_btn(this, 2+16*3+5+5+5,basey
-5, 8, '3');
688 btns
[4]= new cl_btn(this, 2+16*3+5,basey
-5, 16, '4');
694 const char *k
= "asdfghjkqwertyui";
696 for (i
=0, m
=1; i
<16; i
++, m
<<=1)
697 sws
[i
]= new cl_sw(this, 2+16*3-i
*3,basey
+2, m
, k
[15-i
]);
701 cl_n4::draw_fpga(void)
706 io
->dd_cprintf("ui_label", "PB=");
708 io
->dd_printf("%s", board
.c_str());
711 io
->tu_go(2+16*3-i
*3-1,basey
+1);
712 io
->dd_cprintf("ui_label", "%2d", i
);
719 -------------------------------------------------------------------------
722 cl_bool::cl_bool(class cl_uc
*auc
, int aid
, chars aid_string
):
723 cl_n4(auc
, aid
, aid_string
)
730 cl_bool::draw_fpga(void)
737 cl_bool::mk_btns(void)
744 btns
[0]= new cl_btn(this, 2+16*3+5+5 ,basey
-5, 1, '0');
745 btns
[1]= new cl_btn(this, 2+16*3+5+5+5,basey
-5, 2, '1');
746 btns
[2]= new cl_btn(this, 2+16*3+5+5 ,basey
-2, 4, '2');
747 btns
[3]= new cl_btn(this, 2+16*3+5+5+5,basey
-2, 8, '3');
753 -------------------------------------------------------------------------
756 cl_logsys::cl_logsys(class cl_uc
*auc
, int aid
, chars aid_string
):
757 cl_fpga(auc
, aid
, aid_string
)
764 cl_logsys::mk_leds(void)
767 for (i
=0, m
=1; i
<8; i
++, m
<<=1)
768 leds
[i
]= new cl_rgb(this, 2+16*3-i
*3,basey
+7, m
);
772 cl_logsys::mk_segs(void)
775 for (i
=0, d
=0; i
<4; i
++, d
++)
776 segs
[i
]= new cl_seg(this, 2+8*5-i
*5,basey
-6, d
);
780 cl_logsys::draw_fpga(void)
784 io
->tu_go(2+16*3-8*3-1,basey
+7);
785 io
->dd_cprintf("ui_label", "PB=");
787 io
->dd_printf("%s", board
.c_str());
790 io
->tu_go(2+16*3-i
*3-1,basey
+1);
791 io
->dd_cprintf("ui_label", "%2d", i
);
796 cl_logsys::mk_btns(void)
802 btns
[0]= new cl_btn(this, 2+16*3-3-9*3-0*5,basey
+3, 1, '0');
803 btns
[1]= new cl_btn(this, 2+16*3-3-9*3-1*5,basey
+3, 2, '1');
804 btns
[2]= new cl_btn(this, 2+16*3-3-9*3-2*5,basey
+3, 4, '2');
805 btns
[3]= new cl_btn(this, 2+16*3-3-9*3-3*5,basey
+3, 8, '3');
810 cl_logsys::mk_sws(void)
812 const char *k
= "qwertyui";
814 for (i
=0, m
=1; i
<8; i
++, m
<<=1)
815 sws
[i
]= new cl_sw(this, 2+16*3-i
*3,basey
+2, m
, k
[7-i
]);
818 /* End of p1516.src/fpga.cc */