Imported Upstream version 2.9.0
[debian/cc1111] / sim / ucsim / s51.src / port.cc
1 /*
2  * Simulator of microcontrollers (port.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
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.
16
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.
21
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
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include <ctype.h>
29
30 #include "portcl.h"
31 #include "regs51.h"
32 #include "types51.h"
33
34
35 cl_port::cl_port(class cl_uc *auc, int aid):
36   cl_hw(auc, HW_PORT, aid, "port")
37 {
38   port_pins= 0xff;
39 }
40
41 int
42 cl_port::init(void)
43 {
44   switch (id)
45     {
46     case 0: addr_p= P0; break;
47     case 1:
48       {
49         addr_p= P1;
50         /*class cl_hw *hw;
51         if ((hw= uc->get_hw(HW_TIMER, 2, 0)))
52         hws_to_inform->add(hw);*/
53         make_partner(HW_TIMER, 2);
54         make_partner(HW_PCA, 0);
55         break;
56       }
57     case 2: addr_p= P2; break;
58     case 3:
59       {
60         addr_p= P3;
61         //class cl_hw *hw;
62         /*if ((hw= uc->get_hw(HW_TIMER, 0, 0)))
63           hws_to_inform->add(hw);
64         if ((hw= uc->get_hw(HW_TIMER, 1, 0)))
65           hws_to_inform->add(hw);
66         if ((hw= uc->get_hw(HW_DUMMY, 0, 0)))
67         hws_to_inform->add(hw);*/
68         make_partner(HW_TIMER, 0);
69         make_partner(HW_TIMER, 1);
70         make_partner(HW_INTERRUPT, 0);
71         make_partner(HW_DUMMY, 0);
72         break;
73       }
74     default: addr_p= P0; return(1);
75     }
76   class cl_address_space *sfr= uc->address_space(MEM_SFR_ID);
77   if (!sfr)
78     {
79       fprintf(stderr, "No SFR to register port into\n");
80     }
81   //cell_p= sfr->register_hw(addr_p, this, (int*)0);
82   register_cell(sfr, addr_p, &cell_p, wtd_restore_write);
83   prev= cell_p->get();
84   return(0);
85 }
86
87 t_mem
88 cl_port::read(class cl_memory_cell *cell)
89 {
90   //printf("port[%d] read\n",id);
91   return(cell->get() & port_pins);
92 }
93
94 void
95 cl_port::write(class cl_memory_cell *cell, t_mem *val)
96 {
97   struct ev_port_changed ep;
98
99   (*val)&= 0xff; // 8 bit port
100   ep.id= id;
101   ep.addr= addr_p;
102   ep.prev_value= cell_p->get();
103   ep.new_value= *val;
104   ep.pins= ep.new_pins= port_pins;
105   if (ep.prev_value != ep.new_value)
106     inform_partners(EV_PORT_CHANGED, &ep);
107   prev= cell_p->get();
108   //printf("port[%d] write 0x%x\n",id,val);
109 }
110
111 void
112 cl_port::set_cmd(class cl_cmdline *cmdline, class cl_console_base *con)
113 {
114   struct ev_port_changed ep;
115   class cl_cmd_arg *params[1]= { cmdline->param(0) };
116   long value;
117
118   if (cmdline->syntax_match(uc, NUMBER))
119     {
120       value= params[0]->value.number & 0xff;
121
122       ep.id= id;
123       ep.addr= addr_p;
124       ep.pins= port_pins;
125       port_pins= value;
126       ep.prev_value= cell_p->get();
127       ep.new_value= cell_p->get();
128       ep.new_pins= port_pins;
129       if (ep.pins != ep.new_pins)
130         inform_partners(EV_PORT_CHANGED, &ep);
131     }
132   else
133     {
134       con->dd_printf("set hardware port[%d] pins_value\n                   Set port pins\n",
135                      id);
136       value= 0;
137     }
138 }
139
140 /*void
141 cl_port::mem_cell_changed(class cl_m *mem, t_addr addr)
142 {
143   cl_hw::mem_cell_changed(mem, addr);
144   t_mem d= sfr->get();
145   write(sfr, &d);
146 }*/
147
148 void
149 cl_port::print_info(class cl_console_base *con)
150 {
151   uchar data;
152
153   con->dd_printf("%s[%d]\n", id_string, id);
154   data= cell_p->get();
155   con->dd_printf("P%d    ", id);
156   con->print_bin(data, 8);
157   con->dd_printf(" 0x%02x %3d %c (Value in SFR register)\n",
158                  data, data, isprint(data)?data:'.');
159
160   data= /*uc->*/port_pins/*[id]*/;
161   con->dd_printf("Pin%d  ", id);
162   con->print_bin(data, 8);
163   con->dd_printf(" 0x%02x %3d %c (Output of outside circuits)\n",
164                  data, data, isprint(data)?data:'.');
165
166   data= cell_p->read();
167   con->dd_printf("Port%d ", id);
168   con->print_bin(data, 8);
169   con->dd_printf(" 0x%02x %3d %c (Value on the port pins)\n",
170                  data, data, isprint(data)?data:'.');
171 }
172
173
174 /* End of s51.src/port.cc */