5f9c8631fdef6391fa99c71a8b9f19952f30a7d4
[fw/sdcc] / sim / ucsim / s51.src / interrupt.cc
1 /*
2  * Simulator of microcontrollers (interrupt.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 // sim
29 #include "itsrccl.h"
30
31 // local
32 #include "interruptcl.h"
33 #include "regs51.h"
34 //#include "uc51cl.h"
35 #include "types51.h"
36
37
38 cl_interrupt::cl_interrupt(class cl_uc *auc):
39   cl_hw(auc, HW_INTERRUPT, 0, "irq")
40 {
41   was_reti= DD_FALSE;
42 }
43
44 int
45 cl_interrupt::init(void)
46 {
47   class cl_mem *sfr;
48
49   sfr= uc->mem(MEM_SFR);
50   if (sfr)
51     {
52       //sfr->register_hw(IE, this, 0);
53       register_cell(sfr, IE, 0, wtd_restore);
54       register_cell(sfr, TCON, &cell_tcon, wtd_restore_write);
55       bit_INT0= sfr->read(P3) & bm_INT0;
56       bit_INT1= sfr->read(P3) & bm_INT1;
57     }
58   return(0);
59 }
60
61 void
62 cl_interrupt::added_to_uc(void)
63 {
64   uc->it_sources->add(new cl_it_src(bmEX0, TCON, bmIE0, 0x0003, true,
65                                     "external #0", 1));
66   uc->it_sources->add(new cl_it_src(bmEX1, TCON, bmIE1, 0x0013, true,
67                                     "external #1", 3));
68 }
69
70 void
71 cl_interrupt::write(class cl_cell *cell, t_mem *val)
72 {
73   if (cell == cell_tcon)
74     {
75       bit_IT0= *val & bmIT0;
76       bit_IT1= *val & bmIT1;
77     }
78   else
79     // IE register
80     was_reti= DD_TRUE;
81 }
82
83 /*void
84 cl_interrupt::mem_cell_changed(class cl_mem *mem, t_addr addr)
85 {
86 }*/
87
88 int
89 cl_interrupt::tick(int cycles)
90 {
91   if (!bit_IT0 && !bit_INT0)
92     cell_tcon->set_bit1(bmIE0);
93   if (!bit_IT1 && !bit_INT1)
94     cell_tcon->set_bit1(bmIE1);
95   return(resGO);
96 }
97
98 void
99 cl_interrupt::reset(void)
100 {
101   was_reti= DD_FALSE;
102 }
103
104 void
105 cl_interrupt::happen(class cl_hw *where, enum hw_event he, void *params)
106 {
107   struct ev_port_changed *ep= (struct ev_port_changed *)params;
108
109   if (where->cathegory == HW_PORT &&
110       he == EV_PORT_CHANGED &&
111       ep->id == 3)
112     {
113       t_mem p3n= ep->new_pins & ep->new_value;
114       t_mem p3o= ep->pins & ep->prev_value;
115       if (bit_IT0 &&
116           !(p3n & bm_INT0) &&
117           (p3o & bm_INT0))
118         cell_tcon->set_bit1(bmIE0);
119       if (bit_IT1 &&
120           !(p3n & bm_INT1) &&
121           (p3o & bm_INT1))
122         cell_tcon->set_bit1(bmIE1);
123       bit_INT0= p3n & bm_INT0;
124       bit_INT1= p3n & bm_INT1;
125     }
126 }
127
128
129 void
130 cl_interrupt::print_info(class cl_console *con)
131 {
132   int ie= uc->get_mem(MEM_SFR, IE);
133   int i;
134
135   con->dd_printf("Interrupts are %s. Interrupt sources:\n",
136                  (ie&bmEA)?"enabled":"disabled");
137   con->dd_printf("  Handler  En  Pr Req Act Name\n");
138   for (i= 0; i < uc->it_sources->count; i++)
139     {
140       class cl_it_src *is= (class cl_it_src *)(uc->it_sources->at(i));
141       con->dd_printf("  0x%06x", is->addr);
142       con->dd_printf(" %-3s", (ie&(is->ie_mask))?"en":"dis");
143       con->dd_printf(" %2d", uc->it_priority(is->ie_mask));
144       con->dd_printf(" %-3s",
145                      (uc->get_mem(MEM_SFR, is->src_reg)&(is->src_mask))?
146                      "YES":"no");
147       con->dd_printf(" %-3s", (is->active)?"act":"no");
148       con->dd_printf(" %s", is->name);
149       con->dd_printf("\n");
150     }
151   con->dd_printf("Active interrupt service(s):\n");
152   con->dd_printf("  Pr Handler  PC       Source\n");
153   for (i= 0; i < uc->it_levels->count; i++)
154     {
155       class it_level *il= (class it_level *)(uc->it_levels->at(i));
156       if (il->level >= 0)
157         {
158           con->dd_printf("  %2d", il->level);
159           con->dd_printf(" 0x%06x", il->addr);
160           con->dd_printf(" 0x%06x", il->PC);
161           con->dd_printf(" %s", (il->source)?(il->source->name):"nothing");
162           con->dd_printf("\n");
163         }
164     }
165 }
166
167
168 /* End of s51.src/interrupt.cc */