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")
52 serial_in= serial_out= NIL;
55 cl_serial::~cl_serial(void)
59 if (isatty(fileno(serial_out)))
60 tcsetattr(fileno(serial_out), TCSANOW, &saved_attributes_out);
65 if (isatty(fileno(serial_in)))
66 tcsetattr(fileno(serial_in), TCSANOW, &saved_attributes_in);
69 delete serial_in_file_option;
70 delete serial_out_file_option;
79 set_name("mcs51_uart");
80 sfr= uc->address_space(MEM_SFR_ID);
83 //sbuf= sfr->register_hw(SBUF, this, 0);
84 //pcon= sfr->register_hw(PCON, this, 0);
85 //scon= sfr->register_hw(SCON, this, 0);
86 register_cell(sfr, SBUF, &sbuf, wtd_restore_write);
87 register_cell(sfr, PCON, &pcon, wtd_restore_write);
88 register_cell(sfr, SCON, &scon, wtd_restore_write);
91 serial_in_file_option= new cl_optref(this);
92 serial_in_file_option->init();
93 serial_in_file_option->use("serial_in_file");
94 serial_out_file_option= new cl_optref(this);
95 serial_out_file_option->init();
96 serial_out_file_option->use("serial_out_file");
99 /*if ((fni= serial_in_file_option->get_value((char*)0)))
100 serial_in= fopen(fni, "r");
101 if ((fno= serial_out_file_option->get_value((char*)0)))
102 serial_out= fopen(fno, "w");*/
104 //serial_in = (FILE*)application->args->get_parg(0, "Ser_in");
105 //serial_out= (FILE*)application->args->get_parg(0, "Ser_out");
106 serial_in = (FILE*)serial_in_file_option->get_value((void*)0);
107 serial_out= (FILE*)serial_out_file_option->get_value((void*)0);
110 // making `serial' unbuffered
111 if (setvbuf(serial_in, NULL, _IONBF, 0))
112 perror("Unbuffer serial input channel");
113 // setting O_NONBLOCK
114 if ((i= fcntl(fileno(serial_in), F_GETFL, 0)) < 0)
115 perror("Get flags of serial input");
117 if (fcntl(fileno(serial_in), F_SETFL, i) < 0)
118 perror("Set flags of serial input");
119 // switching terminal to noncanonical mode
120 if (isatty(fileno(serial_in)))
122 tcgetattr(fileno(serial_in), &saved_attributes_in);
123 tcgetattr(fileno(serial_in), &tattr);
124 tattr.c_lflag&= ~(ICANON|ECHO);
125 tattr.c_cc[VMIN] = 1;
126 tattr.c_cc[VTIME]= 0;
127 tcsetattr(fileno(serial_in), TCSAFLUSH, &tattr);
130 fprintf(stderr, "Warning: serial input interface connected to a "
131 "non-terminal file.\n");
135 // making `serial' unbuffered
136 if (setvbuf(serial_out, NULL, _IONBF, 0))
137 perror("Unbuffer serial output channel");
138 // setting O_NONBLOCK
139 if ((i= fcntl(fileno(serial_out), F_GETFL, 0)) < 0)
140 perror("Get flags of serial output");
142 if (fcntl(fileno(serial_out), F_SETFL, i) < 0)
143 perror("Set flags of serial output");
144 // switching terminal to noncanonical mode
145 if (isatty(fileno(serial_out)))
147 tcgetattr(fileno(serial_out), &saved_attributes_out);
148 tcgetattr(fileno(serial_out), &tattr);
149 tattr.c_lflag&= ~(ICANON|ECHO);
150 tattr.c_cc[VMIN] = 1;
151 tattr.c_cc[VTIME]= 0;
152 tcsetattr(fileno(serial_out), TCSAFLUSH, &tattr);
155 fprintf(stderr, "Warning: serial output interface connected to a "
156 "non-terminal file.\n");
159 class cl_hw *t2= uc->get_hw(HW_TIMER, 2, 0);
160 if ((there_is_t2= t2 != 0))
162 t_mem d= sfr->get(T2CON);
163 t2_baud= d & (bmRCLK | bmTCLK);
172 cl_serial::new_hw_added(class cl_hw *new_hw)
174 if (new_hw->cathegory == HW_TIMER &&
177 there_is_t2= DD_TRUE;
178 t_mem d= sfr->get(T2CON);
179 t2_baud= d & (bmRCLK | bmTCLK);
184 cl_serial::added_to_uc(void)
186 uc->it_sources->add(new cl_it_src(bmES , SCON, bmTI , 0x0023, false,
187 "serial transmit", 6));
188 uc->it_sources->add(new cl_it_src(bmES , SCON, bmRI , 0x0023, false,
189 "serial receive", 6));
193 cl_serial::read(class cl_memory_cell *cell)
202 cl_serial::write(class cl_memory_cell *cell, t_mem *val)
215 _bmREN= *val & bmREN;
225 _divby= _bmSMOD?16:32;
229 _divby= _bmSMOD?16:32;
233 _divby= _bmSMOD?16:32;
237 else if (cell == pcon)
239 _bmSMOD= *val & bmSMOD;
243 _divby= _bmSMOD?16:32;
246 _divby= _bmSMOD?16:32;
249 _divby= _bmSMOD?16:32;
253 _divby= _bmSMOD?16:32;
258 cl_serial::mem_cell_changed(class cl_m *mem, t_addr addr)
271 cl_serial::serial_bit_cnt(void)
274 int *tr_src= 0, *rec_src= 0;
281 rec_src= &s_rec_tick;
285 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
290 //divby = (/*pcon->get()&bmSMOD*/_bmSMOD)?16:32;
292 rec_src= &s_rec_tick;
299 while (*tr_src >= _divby)
303 //printf("serial bit sent %d\n",uc->ticks->ticks);
308 while (*rec_src >= _divby)
318 cl_serial::tick(int cycles)
322 serial_bit_cnt(/*_mode*/);
327 scon->set_bit1(bmTI);
330 putc(s_out, serial_out);
334 //printf("serial out %d bit rems %d\n",s_tr_bit,uc->ticks->ticks);
336 if ((/*scn & bmREN*/_bmREN) &&
340 fd_set set; static struct timeval timeout= {0,0};
342 FD_SET(fileno(serial_in), &set);
343 int i= select(fileno(serial_in)+1, &set, NULL, NULL, &timeout);
345 FD_ISSET(fileno(serial_in), &set))
347 s_receiving= DD_TRUE;
349 s_rec_tick= /*uc51->*/s_rec_t1= 0;
353 (s_rec_bit >= _bits))
355 if (::read(fileno(serial_in), &c, 1) == 1)
361 s_receiving= DD_FALSE;
366 s_tr_tick+= (l= cycles * uc->clock_per_cycle());
372 cl_serial::received(int c)
374 scon->set_bit1(bmRI);
378 cl_serial::reset(void)
386 s_sending = DD_FALSE;
387 s_receiving= DD_FALSE;
393 cl_serial::happen(class cl_hw *where, enum hw_event he, void *params)
395 if (where->cathegory == HW_TIMER)
399 //printf("serial: timer overflowed %ld\n", uc->ticks->ticks);
403 if (where->id == 2 /*&& there_is_t2*/)
407 case EV_T2_MODE_CHANGED:
410 s_rec_t1= s_tr_t1= 0;
411 t_mem *d= (t_mem *)params;
412 t2_baud= *d & (bmRCLK | bmTCLK);
416 //printf("T2
\abaud ov r%d t%d\n",s_rec_t1,s_tr_t1);
427 cl_serial::print_info(class cl_console *con)
429 char *modes[]= { "Shift, fixed clock",
430 "8 bit UART timer clocked",
431 "9 bit UART fixed clock",
432 "9 bit UART timer clocked" };
435 con->dd_printf("%s[%d]", id_string, id);
436 int mode= (sc&(bmSM0|bmSM1))>>6;
437 con->dd_printf(" %s", modes[mode]);
438 if (mode == 1 || mode == 2)
439 con->dd_printf(" (timer%d)", (t2_baud)?2:1);
440 con->dd_printf(" MultiProc=%s",
441 (mode&2)?((sc&bmSM2)?"ON":"OFF"):"none");
442 con->dd_printf(" irq=%s", (sfr->get(IE)&bmES)?"en":"dis");
443 con->dd_printf(" prio=%d", uc->it_priority(bmPS));
444 con->dd_printf("\n");
446 con->dd_printf("Receiver");
447 con->dd_printf(" %s", (sc&bmREN)?"ON":"OFF");
448 con->dd_printf(" RB8=%c", (sc&bmRB8)?'1':'0');
449 con->dd_printf(" irq=%c", (sc&bmRI)?'1':'0');
450 con->dd_printf("\n");
452 con->dd_printf("Transmitter");
453 con->dd_printf(" TB8=%c", (sc&bmTB8)?'1':'0');
454 con->dd_printf(" irq=%c", (sc&bmTI)?'1':'0');
455 con->dd_printf("\n");
456 /*con->dd_printf("s_rec_t1=%d s_rec_bit=%d s_rec_tick=%d\n",
457 s_rec_t1, s_rec_bit, s_rec_tick);
458 con->dd_printf("s_tr_t1=%d s_tr_bit=%d s_tr_tick=%d\n",
459 s_tr_t1, s_tr_bit, s_tr_tick);
460 con->dd_printf("divby=%d bits=%d\n", _divby, _bits);*/
464 /* End of s51.src/serial.cc */