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
46 cl_serial::cl_serial(class cl_uc *auc):
47 cl_hw(auc, HW_UART, 0, "uart")
50 cl_serial::~cl_serial(void)
54 if (isatty(fileno(serial_out)))
55 tcsetattr(fileno(serial_out), TCSANOW, &saved_attributes_out);
60 if (isatty(fileno(serial_in)))
61 tcsetattr(fileno(serial_in), TCSANOW, &saved_attributes_in);
73 sfr= uc->mem(MEM_SFR);
76 //sbuf= sfr->register_hw(SBUF, this, 0);
77 //pcon= sfr->register_hw(PCON, this, 0);
78 //scon= sfr->register_hw(SCON, this, 0);
79 register_cell(sfr, SBUF, &sbuf, wtd_restore_write);
80 register_cell(sfr, PCON, &pcon, wtd_restore_write);
81 register_cell(sfr, SCON, &scon, wtd_restore_write);
84 serial_in = (FILE*)uc->sim->app->args->get_parg(0, "Ser_in");
85 serial_out= (FILE*)uc->sim->app->args->get_parg(0, "Ser_out");
88 // making `serial' unbuffered
89 if (setvbuf(serial_in, NULL, _IONBF, 0))
90 perror("Unbuffer serial input channel");
92 if ((i= fcntl(fileno(serial_in), F_GETFL, 0)) < 0)
93 perror("Get flags of serial input");
95 if (fcntl(fileno(serial_in), F_SETFL, i) < 0)
96 perror("Set flags of serial input");
97 // switching terminal to noncanonical mode
98 if (isatty(fileno(serial_in)))
100 tcgetattr(fileno(serial_in), &saved_attributes_in);
101 tcgetattr(fileno(serial_in), &tattr);
102 tattr.c_lflag&= ~(ICANON|ECHO);
103 tattr.c_cc[VMIN] = 1;
104 tattr.c_cc[VTIME]= 0;
105 tcsetattr(fileno(serial_in), TCSAFLUSH, &tattr);
108 fprintf(stderr, "Warning: serial input interface connected to a "
109 "non-terminal file.\n");
113 // making `serial' unbuffered
114 if (setvbuf(serial_out, NULL, _IONBF, 0))
115 perror("Unbuffer serial output channel");
116 // setting O_NONBLOCK
117 if ((i= fcntl(fileno(serial_out), F_GETFL, 0)) < 0)
118 perror("Get flags of serial output");
120 if (fcntl(fileno(serial_out), F_SETFL, i) < 0)
121 perror("Set flags of serial output");
122 // switching terminal to noncanonical mode
123 if (isatty(fileno(serial_out)))
125 tcgetattr(fileno(serial_out), &saved_attributes_out);
126 tcgetattr(fileno(serial_out), &tattr);
127 tattr.c_lflag&= ~(ICANON|ECHO);
128 tattr.c_cc[VMIN] = 1;
129 tattr.c_cc[VTIME]= 0;
130 tcsetattr(fileno(serial_out), TCSAFLUSH, &tattr);
133 fprintf(stderr, "Warning: serial output interface connected to a "
134 "non-terminal file.\n");
137 class cl_hw *t2= uc->get_hw(HW_TIMER, 2, 0);
138 if ((there_is_t2= t2 != 0))
140 t_mem d= sfr->get(T2CON);
141 t2_baud= d & (bmRCLK | bmTCLK);
150 cl_serial::new_hw_added(class cl_hw *new_hw)
152 if (new_hw->cathegory == HW_TIMER &&
155 there_is_t2= DD_TRUE;
156 t_mem d= uc->mem(MEM_SFR)->get(T2CON);
157 t2_baud= d & (bmRCLK | bmTCLK);
162 cl_serial::added_to_uc(void)
164 uc->it_sources->add(new cl_it_src(bmES , SCON, bmTI , 0x0023, false,
165 "serial transmit", 6));
166 uc->it_sources->add(new cl_it_src(bmES , SCON, bmRI , 0x0023, false,
167 "serial receive", 6));
171 cl_serial::read(class cl_cell *cell)
180 cl_serial::write(class cl_cell *cell, t_mem *val)
193 _bmREN= *val & bmREN;
203 _divby= _bmSMOD?16:32;
207 _divby= _bmSMOD?16:32;
211 _divby= _bmSMOD?16:32;
215 else if (cell == pcon)
217 _bmSMOD= *val & bmSMOD;
221 _divby= _bmSMOD?16:32;
224 _divby= _bmSMOD?16:32;
227 _divby= _bmSMOD?16:32;
231 _divby= _bmSMOD?16:32;
236 cl_serial::mem_cell_changed(class cl_mem *mem, t_addr addr)
238 class cl_mem *sfr= uc->mem(MEM_SFR);
250 cl_serial::serial_bit_cnt(void)
253 int *tr_src= 0, *rec_src= 0;
260 rec_src= &s_rec_tick;
264 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
269 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
271 rec_src= &s_rec_tick;
278 while (*tr_src >= _divby)
282 //printf("serial bit sent %d\n",uc->ticks->ticks);
287 while (*rec_src >= _divby)
297 cl_serial::tick(int cycles)
301 serial_bit_cnt(/*_mode*/);
306 scon->set_bit1(bmTI);
309 putc(s_out, serial_out);
313 //printf("serial out %d bit rems %d\n",s_tr_bit,uc->ticks->ticks);
315 if ((/*scn & bmREN*/_bmREN) &&
319 fd_set set; static struct timeval timeout= {0,0};
321 FD_SET(fileno(serial_in), &set);
322 int i= select(fileno(serial_in)+1, &set, NULL, NULL, &timeout);
324 FD_ISSET(fileno(serial_in), &set))
326 s_receiving= DD_TRUE;
328 s_rec_tick= /*uc51->*/s_rec_t1= 0;
332 (s_rec_bit >= _bits))
334 if (::read(fileno(serial_in), &c, 1) == 1)
340 s_receiving= DD_FALSE;
345 s_tr_tick+= (l= cycles * uc->clock_per_cycle());
351 cl_serial::received(int c)
353 scon->set_bit1(bmRI);
357 cl_serial::reset(void)
365 s_sending = DD_FALSE;
366 s_receiving= DD_FALSE;
372 cl_serial::happen(class cl_hw *where, enum hw_event he, void *params)
374 if (where->cathegory == HW_TIMER)
378 //printf("serial: timer overflowed %ld\n", uc->ticks->ticks);
382 if (where->id == 2 /*&& there_is_t2*/)
386 case EV_T2_MODE_CHANGED:
389 s_rec_t1= s_tr_t1= 0;
390 t_mem *d= (t_mem *)params;
391 t2_baud= *d & (bmRCLK | bmTCLK);
395 //printf("T2
\abaud ov r%d t%d\n",s_rec_t1,s_tr_t1);
406 cl_serial::print_info(class cl_console *con)
408 char *modes[]= { "Shift, fixed clock",
409 "8 bit UART timer clocked",
410 "9 bit UART fixed clock",
411 "9 bit UART timer clocked" };
414 con->dd_printf("%s[%d]", id_string, id);
415 int mode= (sc&(bmSM0|bmSM1))>>6;
416 con->dd_printf(" %s", modes[mode]);
417 if (mode == 1 || mode == 2)
418 con->dd_printf(" (timer%d)", (t2_baud)?2:1);
419 con->dd_printf(" MultiProc=%s",
420 (mode&2)?((sc&bmSM2)?"ON":"OFF"):"none");
421 con->dd_printf(" irq=%s", (uc->get_mem(MEM_SFR, IE)&bmES)?"en":"dis");
422 con->dd_printf(" prio=%d", uc->it_priority(bmPS));
423 con->dd_printf("\n");
425 con->dd_printf("Receiver");
426 con->dd_printf(" %s", (sc&bmREN)?"ON":"OFF");
427 con->dd_printf(" RB8=%c", (sc&bmRB8)?'1':'0');
428 con->dd_printf(" irq=%c", (sc&bmRI)?'1':'0');
429 con->dd_printf("\n");
431 con->dd_printf("Transmitter");
432 con->dd_printf(" TB8=%c", (sc&bmTB8)?'1':'0');
433 con->dd_printf(" irq=%c", (sc&bmTI)?'1':'0');
434 con->dd_printf("\n");
435 /*con->dd_printf("s_rec_t1=%d s_rec_bit=%d s_rec_tick=%d\n",
436 s_rec_t1, s_rec_bit, s_rec_tick);
437 con->dd_printf("s_tr_t1=%d s_tr_bit=%d s_tr_tick=%d\n",
438 s_tr_t1, s_tr_bit, s_tr_tick);
439 con->dd_printf("divby=%d bits=%d\n", _divby, _bits);*/
443 /* End of s51.src/serial.cc */