updown.cpp revision 12855:588919e0e4aa
1#include "systemc.h" 2#include "specialized_signals/scx_signal_int.h" 3#include "specialized_signals/scx_signal_signed.h" 4#include "specialized_signals/scx_signal_uint.h" 5#include "specialized_signals/scx_signal_unsigned.h" 6 7SC_MODULE(up_down) 8{ 9 sc_in_clk clk; 10 sc_in<sc_uint<1> > up; 11 sc_in<sc_uint<1> > down; 12 sc_in<sc_uint<9> > data_in; 13 sc_inout<sc_uint<1> > parity_out; 14 sc_inout<sc_uint<1> > carry_out; 15 sc_inout<sc_uint<1> > borrow_out; 16 sc_inout<sc_uint<9> > count_out; 17 18 sc_uint<10> cnt_dn; 19 sc_uint<10> cnt_up; 20 sc_uint<9> count_nxt; 21 sc_uint<1> load; 22 23 SC_CTOR(up_down) 24 { 25 SC_METHOD(run); 26 sensitive << clk.pos(); 27 } 28 29 void run() 30 { 31 cnt_dn = count_out - 5; 32 cnt_up = count_out + 3; 33 34 load = 1; 35 switch( (unsigned int )(up,down) ) { 36 case(0): 37 count_nxt = data_in; 38 break; 39 case(1): 40 count_nxt = cnt_dn; 41 break; 42 case(2): 43 count_nxt = cnt_up; 44 break; 45 case(3): 46 load = 0; 47 break; 48 } 49 50 if( load) { 51 parity_out = count_nxt.xor_reduce(); 52 carry_out = up&cnt_up[9]; 53 borrow_out = down&cnt_dn[9]; 54 count_out = count_nxt; 55 } 56 } 57}; 58 59 60#define UP_DOWN(up, down) up, down 61 62struct stimulus { 63 int up; 64 int down; 65 int data_in; 66} s[] = { 67 { UP_DOWN(0, 0), 200 }, /* load 200 */ 68 { UP_DOWN(1, 0), 0 }, /* inc */ 69 { UP_DOWN(1, 0), 0 }, /* inc */ 70 { UP_DOWN(0, 1), 0 }, /* dec */ 71 { UP_DOWN(0, 1), 0 }, /* dec */ 72 { UP_DOWN(0, 1), 0 }, /* dec */ 73 { UP_DOWN(1, 1), 0 }, /* hold */ 74 { UP_DOWN(1, 1), 0 }, /* hold */ 75 { UP_DOWN(1, 1), 0 }, /* hold */ 76 { UP_DOWN(0, 0), 200 }, /* load 200 */ 77 { UP_DOWN(1, 1), 0 }, /* hold */ 78 { UP_DOWN(1, 1), 0 }, /* hold */ 79 { UP_DOWN(0, 1), 0 }, /* dec */ 80 { UP_DOWN(0, 0), 2 }, /* load 2 */ 81 { UP_DOWN(0, 1), 0 }, /* dec */ 82 { UP_DOWN(0, 0), 0x1ff},/* load 0x1ff */ 83 { UP_DOWN(1, 0), 0 }, /* inc */ 84}; 85 86 87int sc_main(int argc, char* argv[]) 88{ 89 sc_signal<sc_uint<1> > borrow_out; 90 sc_signal<sc_uint<1> > carry_out; 91 sc_clock clock; 92 sc_signal<sc_uint<9> > count_out; 93 sc_signal<sc_uint<9> > data_in; 94 sc_signal<sc_uint<1> > down; 95 unsigned int i; 96 sc_signal<sc_uint<1> > parity_out; 97 sc_signal<sc_uint<1> > up; 98 99 up_down up_down_0("up_down_0"); 100 up_down_0.borrow_out(borrow_out); 101 up_down_0.carry_out(carry_out); 102 up_down_0.data_in(data_in); 103 up_down_0.clk(clock); 104 up_down_0.count_out(count_out); 105 up_down_0.down(down); 106 up_down_0.parity_out(parity_out); 107 up_down_0.up(up); 108 109 printf("%5s %2s %4s %7s %10s %8s %10s %9s\n", 110 "clock", "up", "down", "data_in", "parity_out", 111 "carry_out", "borrow_out", "count_out"); 112 113 for( i = 0; i < sizeof s/sizeof(struct stimulus); i++) { 114 up = s[i].up; 115 down = s[i].down; 116 data_in = s[i].data_in; 117 118 sc_start(1, SC_NS); 119 120 printf("%5d %2d %4d %7d %10d %8d %10d %9d\n", 121 (int)sc_time_stamp().to_double()/1000, (int)up, (int)down, 122 (int)data_in, (int)parity_out, (int)carry_out, (int)borrow_out, 123 (int)count_out); 124 } 125 126 /* get last register values */ 127 printf("%5d %2d %4d %7d %10d %8d %10d %9d\n", 128 (int)sc_time_stamp().to_double()/1000, (int)up, (int)down, 129 (int)data_in, (int)parity_out, (int)carry_out, (int)borrow_out, 130 (int)count_out); 131 132 return 0; 133} 134 135