1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* Simulator of microcontrollers (sim.src/serial_hw.cc)
*
* Copyright (C) 2016,16 Drotos Daniel, Talker Bt.
*
* To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
*
*/
/* This file is part of microcontroller simulator: ucsim.
UCSIM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
UCSIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UCSIM; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
/*@1@*/
/* $Id: serial_hw.cc 9943 2017-07-01 16:16:20Z drdani $ */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "utils.h"
#include "globals.h"
#include "fiocl.h"
#include "serial_hwcl.h"
cl_serial_hw::cl_serial_hw(class cl_uc *auc, int aid, chars aid_string):
cl_hw(auc, HW_UART, aid, (const char *)aid_string)
{
listener= 0;
}
cl_serial_hw::~cl_serial_hw(void)
{
delete serial_in_file_option;
delete serial_out_file_option;
delete io;
}
int
cl_serial_hw::init(void)
{
char *s;
cl_hw::init();
make_io();
input_avail= false;
s= format_string("serial%d_in_file", id);
serial_in_file_option= new cl_optref(this);
serial_in_file_option->init();
serial_in_file_option->use(s);
free(s);
s= format_string("serial%d_out_file", id);
serial_out_file_option= new cl_optref(this);
serial_out_file_option->init();
serial_out_file_option->use(s);
free(s);
s= format_string("serial%d_port", id);
serial_port_option= new cl_optref(this);
serial_port_option->init();
class cl_option *o= serial_port_option->use(s);
free(s);
int port= -1;
if (o)
{
port= serial_port_option->get_value((long)0);
if (port < 0)
;//port= 5560+id;
}
//else port= 5560+id;
if (port > 0)
{
listener= new cl_serial_listener(port, application, this);
class cl_commander_base *c= application->get_commander();
c->add_console(listener);
}
char *f_serial_in = (char*)serial_in_file_option->get_value((char*)0);
char *f_serial_out= (char*)serial_out_file_option->get_value((char*)0);
class cl_f *fi, *fo;
if (f_serial_in)
{
if (f_serial_in[0] == '\001')
fi= (class cl_f *)(strtoll(&f_serial_in[1], 0, 0));
else
fi= mk_io(chars(f_serial_in), cchars("r"));
if (!fi->tty)
fprintf(stderr, "Warning: serial input interface connected to a "
"non-terminal file.\n");
}
else
fi= 0;//mk_io(chars(""), chars(""));
if (f_serial_out)
{
if (f_serial_out[0] == '\001')
fo= (class cl_f *)(strtoll(&f_serial_out[1], 0, 0));
else
fo= mk_io(chars(f_serial_out), "w");
if (!fo->tty)
fprintf(stderr, "Warning: serial output interface connected to a "
"non-terminal file.\n");
}
else
fo= 0;//mk_io(chars(""), chars(""));
io->replace_files(true, fi, fo);
if (fi)
{
fi->interactive(NULL);
fi->raw();
fi->echo(NULL);
}
menu= 0;
cfg_set(serconf_on, true);
cfg_set(serconf_check_often, false);
cfg_set(serconf_escape, 'x'-'a'+1);
cl_var *v;
chars pn(id_string);
pn.append("%d_", id);
uc->vars->add(v= new cl_var(pn+chars("on"), cfg, serconf_on,
"WR: turn on/off, RD: check state"));
v->init();
uc->vars->add(v= new cl_var(pn+chars("check_often"), cfg, serconf_check_often,
"When true, serial IO checked at every instruction"));
v->init();
uc->vars->add(v= new cl_var(pn+chars("esc_char"), cfg, serconf_escape,
"Escape character on serial IO screen"));
v->init();
return 0;
}
t_mem
cl_serial_hw::conf_op(cl_memory_cell *cell, t_addr addr, t_mem *val)
{
switch ((enum serial_cfg)addr)
{
case serconf_on: // turn this HW on/off
if (val)
{
if (*val)
on= true;
else
on= false;
}
else
{
cell->set(on?1:0);
}
break;
case serconf_check_often:
if (val)
{
cell->set(*val?1:0);
}
break;
case serconf_escape:
if (val)
{
char c= tolower(*val);
if ((c >= 'a') &&
(c <= 'z'))
cell->set(c - 'a'+1);
}
default:
break;
}
return cell->get();
}
void
cl_serial_hw::make_io()
{
if (!io)
{
io= new cl_serial_io(this);
application->get_commander()->add_console(io);
}
}
void
cl_serial_hw::new_io(class cl_f *f_in, class cl_f *f_out)
{
char esc= (char)cfg_get(serconf_escape);
cl_hw::new_io(f_in, f_out);
if (io)
io->dd_printf("%s[%d] terminal display, press ^%c to access control menu\n",
id_string, id,
'a'+esc-1);
menu= 0;
}
bool
cl_serial_hw::proc_input(void)
{
int c;
char esc= (char)cfg_get(serconf_escape);
bool run= uc->sim->state & SIM_GO;
class cl_f *fin, *fout;
fin= io->get_fin();
fout= io->get_fout();
if (fin->eof())
{
if (fout &&
(fout->file_id == fin->file_id))
{
delete fout;
io->replace_files(false, fin, 0);
fout= 0;
}
delete fin;
io->replace_files(false, 0, fout);
return true;
}
if (menu == 0)
{
if (fin->tty)
{
if (fin->read(&c, 1))
{
if (c == esc)
{
menu= 'm';
io->dd_printf("\n"
"Simulator control menu\n"
" %c Insert ^%c\n"
" s,r,g Start simulation\n"
" p Stop simulation\n"
" T Reset CPU\n"
" q Quit simulator\n"
" o Close serial terminal\n"
" e Exit menu\n"
" n Change display\n"
,
'a'+esc-1, 'a'+esc-1
);
}
else if (!input_avail)
{
input= c;
input_avail= true;
}
}
}
else if (!input_avail)
{
if (fin->read(&c, 1))
{
input= c;
input_avail= true;
}
}
}
else
{
if (fin->read(&c, 1))
{
switch (menu)
{
case 'm':
if ((c == esc-1+'a') ||
(c == esc-1+'A') ||
(c == esc))
{
// insert ^esc
if (run && !input_avail)
{
input= esc, input_avail= true;
io->dd_printf("^%c interted.\n", 'a'+esc-1);
}
else
io->dd_printf("Control menu exited.\n");
menu= 0;
}
switch (c)
{
case 'e': case 'E': case 'e'-'a'+1:
// exit menu
menu= 0;
io->dd_printf("Control menu exited.\n");
break;
case 's': case 'S': case 's'-'a'+1:
case 'r': case 'R': case 'r'-'a'+1:
case 'g': case 'G': case 'g'-'a'+1:
// start
uc->sim->start(0, 0);
menu= 0;
io->dd_printf("Simulation started.\n");
break;
case 'p': case 'P': case 'p'-'a'+1:
uc->sim->stop(resSIMIF);
// stop
menu= 0;
io->dd_printf("Simulation stopped.\n");
break;
case 'T':
uc->reset();
menu= 0;
io->dd_printf("CPU reset.\n");
break;
case 'q': case 'Q': case 'q'-'a'+1:
// kill
uc->sim->state|= SIM_QUIT;
menu= 0;
io->dd_printf("Exit simulator.\n");
break;
case 'o': case 'O': case 'o'-'a'+1:
{
// close
io->dd_printf("Closing terminal.\n");
menu= 0;
io->convert2console();
break;
}
case 'n': case 'N': case 'n'-'a'+1:
{
class cl_hw *h= next_displayer();
if (!h)
io->dd_printf("No other displayer.\n");
else
{
io->tu_reset();
io->tu_cls();
io->pass2hw(h);
}
menu= 0;
break;
}
default:
menu= 0;
io->dd_printf("Control menu closed (%d).\n", c);
break;
}
break;
}
}
}
return true;
}
cl_serial_listener::cl_serial_listener(int serverport, class cl_app *the_app,
class cl_serial_hw *the_serial):
cl_listen_console(serverport, the_app)
{
serial_hw= the_serial;
}
int
cl_serial_listener::init(void)
{
if (serial_hw)
set_name(chars("", "serial_listener_%s_%d\n", serial_hw->get_name(), serial_hw->id));
return 0;
}
int
cl_serial_listener::proc_input(class cl_cmdset *cmdset)
{
class cl_f *i, *o;
srv_accept(fin, &i, &o);
i->set_telnet(true);
serial_hw->new_io(i, o);
return 0;
}
/* End of sim.src/serial_hw.cc */
|