1 |
2 |
sinclairrf |
################################################################################
|
2 |
|
|
#
|
3 |
|
|
# Copyright 2012-2013, Sinclair R.F., Inc.
|
4 |
|
|
#
|
5 |
|
|
################################################################################
|
6 |
|
|
|
7 |
|
|
from ssbccPeripheral import SSBCCperipheral
|
8 |
|
|
|
9 |
|
|
class adder_16bit(SSBCCperipheral):
|
10 |
|
|
"""The "adder_16bit" peripheral adds or subtracts two 16 bit values.\n
|
11 |
|
|
Usage:
|
12 |
|
|
PERIPHERAL adder_16bit\n
|
13 |
|
|
The following OUTPORTs are provided by the peripheral:
|
14 |
|
|
port description
|
15 |
|
|
O_ADDER_16BIT_MSB1 MSB of first argument
|
16 |
|
|
O_ADDER_16BIT_LSB1 LSB of first argument
|
17 |
|
|
O_ADDER_16BIT_MSB2 MSB of second argument
|
18 |
|
|
O_ADDER_16BIT_LSB2 LSB of second argument
|
19 |
|
|
O_ADDER_16BIT_OP 0 ==> add, 1 ==> subtract\n
|
20 |
|
|
The following INPORTs are provided by the peripheral:
|
21 |
|
|
port description
|
22 |
|
|
I_ADDER_16BIT_MSB MSB of the sum/difference
|
23 |
|
|
I_ADDER_16BIT_LSB LSB of the sum/difference\n
|
24 |
|
|
Example: Incorporate the peripheral:\n
|
25 |
|
|
Example: Add an 8-bit value and a 16-bit value from the stack:\n
|
26 |
|
|
Within the processor architecture file include the configuration command:\n
|
27 |
|
|
PERIPHERAL adder_16bit\n
|
28 |
|
|
Use the following assembly code to perform the addition to implement a
|
29 |
|
|
function that adds an 8-bit value at the top of the data stack to the 16-bit
|
30 |
|
|
value immediately below it:\n
|
31 |
|
|
; ( u2_LSB u2_MSB u1 - (u1+u2)_LSB (u1+u2)_MSB
|
32 |
|
|
.function add_u8_u16__u16
|
33 |
|
|
; write the 8-bit value to the peripheral (after converting it to a 16 bit
|
34 |
|
|
; value)
|
35 |
|
|
|
36 |
|
|
; write the 16-bit value to the peripheral
|
37 |
|
|
.outport(O_ADDER_16BIT_MSB2) .outport(O_ADDER_16BIT_LSB2)
|
38 |
|
|
; command an addition
|
39 |
|
|
|
40 |
|
|
; push the 16-bit sum onto the stack
|
41 |
|
|
.inport(I_ADDER_16BIT_LSB) .inport(I_ADDER_16BIT_MSB)
|
42 |
|
|
.return
|
43 |
|
|
"""
|
44 |
|
|
|
45 |
|
|
def __init__(self,peripheralFile,config,params,loc):
|
46 |
|
|
# Use the externally provided file name for the peripheral
|
47 |
|
|
self.peripheralFile = peripheralFile;
|
48 |
|
|
# List the signals to be declared for the peripheral.
|
49 |
|
|
config.AddSignal('s__adder_16bit_out_MSB',8,loc);
|
50 |
|
|
config.AddSignal('s__adder_16bit_out_LSB',8,loc);
|
51 |
|
|
config.AddSignal('s__adder_16bit_in_MSB1',8,loc);
|
52 |
|
|
config.AddSignal('s__adder_16bit_in_LSB1',8,loc);
|
53 |
|
|
config.AddSignal('s__adder_16bit_in_MSB2',8,loc);
|
54 |
|
|
config.AddSignal('s__adder_16bit_in_LSB2',8,loc);
|
55 |
|
|
config.AddSignal('s__adder_16bit_in_op',1,loc);
|
56 |
|
|
# List the input ports to the peripheral.
|
57 |
|
|
config.AddInport(('I_ADDER_16BIT_MSB',
|
58 |
|
|
('s__adder_16bit_out_MSB',8,'data',),
|
59 |
|
|
),loc);
|
60 |
|
|
config.AddInport(('I_ADDER_16BIT_LSB',
|
61 |
|
|
('s__adder_16bit_out_LSB',8,'data',),
|
62 |
|
|
),loc);
|
63 |
|
|
# List the output ports from the peripheral.
|
64 |
|
|
config.AddOutport(('O_ADDER_16BIT_MSB1',False,
|
65 |
|
|
('s__adder_16bit_in_MSB1',8,'data',),
|
66 |
|
|
),loc);
|
67 |
|
|
config.AddOutport(('O_ADDER_16BIT_LSB1',False,
|
68 |
|
|
('s__adder_16bit_in_LSB1',8,'data',),
|
69 |
|
|
),loc);
|
70 |
|
|
config.AddOutport(('O_ADDER_16BIT_MSB2',False,
|
71 |
|
|
('s__adder_16bit_in_MSB2',8,'data',),
|
72 |
|
|
),loc);
|
73 |
|
|
config.AddOutport(('O_ADDER_16BIT_LSB2',False,
|
74 |
|
|
('s__adder_16bit_in_LSB2',8,'data',),
|
75 |
|
|
),loc);
|
76 |
|
|
config.AddOutport(('O_ADDER_16BIT_OP',False,
|
77 |
|
|
('s__adder_16bit_in_op',1,'data',),
|
78 |
|
|
),loc);
|
79 |
|
|
|
80 |
|
|
def GenAssembly(self,config):
|
81 |
|
|
fp = file('adder_16bit.s','w');
|
82 |
|
|
fp.write("""; Copyright 2012-2013, Sinclair R.F., Inc.
|
83 |
|
|
; adder_16bit.s
|
84 |
|
|
; library to facilitate using the 16-bit adder peripheral
|
85 |
|
|
|
86 |
|
|
; ( u_1_LSB u_1_MSB u_2_LSB u_2_MSB u_op - (u_1+u_2)_LSB (u_1+u_2)_MSB )
|
87 |
|
|
.function addsub_u16_u16__u16
|
88 |
|
|
.outport(O_ADDER_16BIT_OP)
|
89 |
|
|
.outport(O_ADDER_16BIT_MSB2) .outport(O_ADDER_16BIT_LSB2)
|
90 |
|
|
.outport(O_ADDER_16BIT_MSB1) .outport(O_ADDER_16BIT_LSB1)
|
91 |
|
|
.inport(I_ADDER_16BIT_LSB) I_ADDER_16BIT_MSB
|
92 |
|
|
.return(inport)
|
93 |
|
|
""");
|
94 |
|
|
|
95 |
|
|
def GenVerilog(self,fp,config):
|
96 |
|
|
body = """//
|
97 |
|
|
// PERIPHERAL adder_16bit:
|
98 |
|
|
//
|
99 |
|
|
always @ (posedge i_clk)
|
100 |
|
|
if (s__adder_16bit_in_op == 1\'b0)
|
101 |
|
|
{ s__adder_16bit_out_MSB, s__adder_16bit_out_LSB }
|
102 |
|
|
<= { s__adder_16bit_in_MSB1, s__adder_16bit_in_LSB1 }
|
103 |
|
|
+ { s__adder_16bit_in_MSB2, s__adder_16bit_in_LSB2 };
|
104 |
|
|
else
|
105 |
|
|
{ s__adder_16bit_out_MSB, s__adder_16bit_out_LSB }
|
106 |
|
|
<= { s__adder_16bit_in_MSB1, s__adder_16bit_in_LSB1 }
|
107 |
|
|
- { s__adder_16bit_in_MSB2, s__adder_16bit_in_LSB2 };
|
108 |
|
|
""";
|
109 |
|
|
body = self.GenVerilogFinal(config,body);
|
110 |
|
|
fp.write(body);
|