2 * Simulator of microcontrollers (serial.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
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
49 cl_serial::cl_serial(class cl_uc *auc):
50 cl_hw(auc, HW_UART, 0, "uart")
53 cl_serial::~cl_serial(void)
57 if (isatty(fileno(serial_out)))
58 tcsetattr(fileno(serial_out), TCSANOW, &saved_attributes_out);
63 if (isatty(fileno(serial_in)))
64 tcsetattr(fileno(serial_in), TCSANOW, &saved_attributes_in);
76 sfr= uc->mem(MEM_SFR);
79 //sbuf= sfr->register_hw(SBUF, this, 0);
80 //pcon= sfr->register_hw(PCON, this, 0);
81 //scon= sfr->register_hw(SCON, this, 0);
82 register_cell(sfr, SBUF, &sbuf, wtd_restore_write);
83 register_cell(sfr, PCON, &pcon, wtd_restore_write);
84 register_cell(sfr, SCON, &scon, wtd_restore_write);
87 serial_in = (FILE*)application->args->get_parg(0, "Ser_in");
88 serial_out= (FILE*)application->args->get_parg(0, "Ser_out");
91 // making `serial' unbuffered
92 if (setvbuf(serial_in, NULL, _IONBF, 0))
93 perror("Unbuffer serial input channel");
95 if ((i= fcntl(fileno(serial_in), F_GETFL, 0)) < 0)
96 perror("Get flags of serial input");
98 if (fcntl(fileno(serial_in), F_SETFL, i) < 0)
99 perror("Set flags of serial input");
100 // switching terminal to noncanonical mode
101 if (isatty(fileno(serial_in)))
103 tcgetattr(fileno(serial_in), &saved_attributes_in);
104 tcgetattr(fileno(serial_in), &tattr);
105 tattr.c_lflag&= ~(ICANON|ECHO);
106 tattr.c_cc[VMIN] = 1;
107 tattr.c_cc[VTIME]= 0;
108 tcsetattr(fileno(serial_in), TCSAFLUSH, &tattr);
111 fprintf(stderr, "Warning: serial input interface connected to a "
112 "non-terminal file.\n");
116 // making `serial' unbuffered
117 if (setvbuf(serial_out, NULL, _IONBF, 0))
118 perror("Unbuffer serial output channel");
119 // setting O_NONBLOCK
120 if ((i= fcntl(fileno(serial_out), F_GETFL, 0)) < 0)
121 perror("Get flags of serial output");
123 if (fcntl(fileno(serial_out), F_SETFL, i) < 0)
124 perror("Set flags of serial output");
125 // switching terminal to noncanonical mode
126 if (isatty(fileno(serial_out)))
128 tcgetattr(fileno(serial_out), &saved_attributes_out);
129 tcgetattr(fileno(serial_out), &tattr);
130 tattr.c_lflag&= ~(ICANON|ECHO);
131 tattr.c_cc[VMIN] = 1;
132 tattr.c_cc[VTIME]= 0;
133 tcsetattr(fileno(serial_out), TCSAFLUSH, &tattr);
136 fprintf(stderr, "Warning: serial output interface connected to a "
137 "non-terminal file.\n");
140 class cl_hw *t2= uc->get_hw(HW_TIMER, 2, 0);
141 if ((there_is_t2= t2 != 0))
143 t_mem d= sfr->get(T2CON);
144 t2_baud= d & (bmRCLK | bmTCLK);
153 cl_serial::new_hw_added(class cl_hw *new_hw)
155 if (new_hw->cathegory == HW_TIMER &&
158 there_is_t2= DD_TRUE;
159 t_mem d= uc->mem(MEM_SFR)->get(T2CON);
160 t2_baud= d & (bmRCLK | bmTCLK);
165 cl_serial::added_to_uc(void)
167 uc->it_sources->add(new cl_it_src(bmES , SCON, bmTI , 0x0023, false,
168 "serial transmit", 6));
169 uc->it_sources->add(new cl_it_src(bmES , SCON, bmRI , 0x0023, false,
170 "serial receive", 6));
174 cl_serial::read(class cl_cell *cell)
183 cl_serial::write(class cl_cell *cell, t_mem *val)
196 _bmREN= *val & bmREN;
206 _divby= _bmSMOD?16:32;
210 _divby= _bmSMOD?16:32;
214 _divby= _bmSMOD?16:32;
218 else if (cell == pcon)
220 _bmSMOD= *val & bmSMOD;
224 _divby= _bmSMOD?16:32;
227 _divby= _bmSMOD?16:32;
230 _divby= _bmSMOD?16:32;
234 _divby= _bmSMOD?16:32;
239 cl_serial::mem_cell_changed(class cl_mem *mem, t_addr addr)
241 class cl_mem *sfr= uc->mem(MEM_SFR);
253 cl_serial::serial_bit_cnt(void)
256 int *tr_src= 0, *rec_src= 0;
263 rec_src= &s_rec_tick;
267 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
272 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
274 rec_src= &s_rec_tick;
281 while (*tr_src >= _divby)
285 //printf("serial bit sent %d\n",uc->ticks->ticks);
290 while (*rec_src >= _divby)
300 cl_serial::tick(int cycles)
304 serial_bit_cnt(/*_mode*/);
309 scon->set_bit1(bmTI);
312 putc(s_out, serial_out);
316 //printf("serial out %d bit rems %d\n",s_tr_bit,uc->ticks->ticks);
318 if ((/*scn & bmREN*/_bmREN) &&
322 fd_set set; static struct timeval timeout= {0,0};
324 FD_SET(fileno(serial_in), &set);
325 int i= select(fileno(serial_in)+1, &set, NULL, NULL, &timeout);
327 FD_ISSET(fileno(serial_in), &set))
329 s_receiving= DD_TRUE;
331 s_rec_tick= /*uc51->*/s_rec_t1= 0;
335 (s_rec_bit >= _bits))
337 if (::read(fileno(serial_in), &c, 1) == 1)
343 s_receiving= DD_FALSE;
348 s_tr_tick+= (l= cycles * uc->clock_per_cycle());
354 cl_serial::received(int c)
356 scon->set_bit1(bmRI);
360 cl_serial::reset(void)
368 s_sending = DD_FALSE;
369 s_receiving= DD_FALSE;
375 cl_serial::happen(class cl_hw *where, enum hw_event he, void *params)
377 if (where->cathegory == HW_TIMER)
381 //printf("serial: timer overflowed %ld\n", uc->ticks->ticks);
385 if (where->id == 2 /*&& there_is_t2*/)
389 case EV_T2_MODE_CHANGED:
392 s_rec_t1= s_tr_t1= 0;
393 t_mem *d= (t_mem *)params;
394 t2_baud= *d & (bmRCLK | bmTCLK);
398 //printf("T2
\abaud ov r%d t%d\n",s_rec_t1,s_tr_t1);
409 cl_serial::print_info(class cl_console *con)
411 char *modes[]= { "Shift, fixed clock",
412 "8 bit UART timer clocked",
413 "9 bit UART fixed clock",
414 "9 bit UART timer clocked" };
417 con->dd_printf("%s[%d]", id_string, id);
418 int mode= (sc&(bmSM0|bmSM1))>>6;
419 con->dd_printf(" %s", modes[mode]);
420 if (mode == 1 || mode == 2)
421 con->dd_printf(" (timer%d)", (t2_baud)?2:1);
422 con->dd_printf(" MultiProc=%s",
423 (mode&2)?((sc&bmSM2)?"ON":"OFF"):"none");
424 con->dd_printf(" irq=%s", (uc->get_mem(MEM_SFR, IE)&bmES)?"en":"dis");
425 con->dd_printf(" prio=%d", uc->it_priority(bmPS));
426 con->dd_printf("\n");
428 con->dd_printf("Receiver");
429 con->dd_printf(" %s", (sc&bmREN)?"ON":"OFF");
430 con->dd_printf(" RB8=%c", (sc&bmRB8)?'1':'0');
431 con->dd_printf(" irq=%c", (sc&bmRI)?'1':'0');
432 con->dd_printf("\n");
434 con->dd_printf("Transmitter");
435 con->dd_printf(" TB8=%c", (sc&bmTB8)?'1':'0');
436 con->dd_printf(" irq=%c", (sc&bmTI)?'1':'0');
437 con->dd_printf("\n");
438 /*con->dd_printf("s_rec_t1=%d s_rec_bit=%d s_rec_tick=%d\n",
439 s_rec_t1, s_rec_bit, s_rec_tick);
440 con->dd_printf("s_tr_t1=%d s_tr_bit=%d s_tr_tick=%d\n",
441 s_tr_t1, s_tr_bit, s_tr_tick);
442 con->dd_printf("divby=%d bits=%d\n", _divby, _bits);*/
446 /* End of s51.src/serial.cc */