1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/lm32_adder.v Sun Apr 04 20:40:03 2010 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +// ============================================================================= 1.5 +// COPYRIGHT NOTICE 1.6 +// Copyright 2006 (c) Lattice Semiconductor Corporation 1.7 +// ALL RIGHTS RESERVED 1.8 +// This confidential and proprietary software may be used only as authorised by 1.9 +// a licensing agreement from Lattice Semiconductor Corporation. 1.10 +// The entire notice above must be reproduced on all authorized copies and 1.11 +// copies may only be made to the extent permitted by a licensing agreement from 1.12 +// Lattice Semiconductor Corporation. 1.13 +// 1.14 +// Lattice Semiconductor Corporation TEL : 1-800-Lattice (USA and Canada) 1.15 +// 5555 NE Moore Court 408-826-6000 (other locations) 1.16 +// Hillsboro, OR 97124 web : http://www.latticesemi.com/ 1.17 +// U.S.A email: techsupport@latticesemi.com 1.18 +// ============================================================================/ 1.19 +// FILE DETAILS 1.20 +// Project : LatticeMico32 1.21 +// File : lm32_adder.v 1.22 +// Title : Integer adder / subtractor with comparison flag generation 1.23 +// Dependencies : lm32_include.v 1.24 +// Version : 6.1.17 1.25 +// : Initial Release 1.26 +// Version : 7.0SP2, 3.0 1.27 +// : No Change 1.28 +// Version : 3.1 1.29 +// : No Change 1.30 +// ============================================================================= 1.31 + 1.32 +`include "lm32_include.v" 1.33 + 1.34 +///////////////////////////////////////////////////// 1.35 +// Module interface 1.36 +///////////////////////////////////////////////////// 1.37 + 1.38 +module lm32_adder ( 1.39 + // ----- Inputs ------- 1.40 + adder_op_x, 1.41 + adder_op_x_n, 1.42 + operand_0_x, 1.43 + operand_1_x, 1.44 + // ----- Outputs ------- 1.45 + adder_result_x, 1.46 + adder_carry_n_x, 1.47 + adder_overflow_x 1.48 + ); 1.49 + 1.50 +///////////////////////////////////////////////////// 1.51 +// Inputs 1.52 +///////////////////////////////////////////////////// 1.53 + 1.54 +input adder_op_x; // Operating to perform, 0 for addition, 1 for subtraction 1.55 +input adder_op_x_n; // Inverted version of adder_op_x 1.56 +input [`LM32_WORD_RNG] operand_0_x; // Operand to add, or subtract from 1.57 +input [`LM32_WORD_RNG] operand_1_x; // Opearnd to add, or subtract by 1.58 + 1.59 +///////////////////////////////////////////////////// 1.60 +// Outputs 1.61 +///////////////////////////////////////////////////// 1.62 + 1.63 +output [`LM32_WORD_RNG] adder_result_x; // Result of addition or subtraction 1.64 +wire [`LM32_WORD_RNG] adder_result_x; 1.65 +output adder_carry_n_x; // Inverted carry 1.66 +wire adder_carry_n_x; 1.67 +output adder_overflow_x; // Indicates if overflow occured, only valid for subtractions 1.68 +reg adder_overflow_x; 1.69 + 1.70 +///////////////////////////////////////////////////// 1.71 +// Internal nets and registers 1.72 +///////////////////////////////////////////////////// 1.73 + 1.74 +wire a_sign; // Sign (i.e. positive or negative) of operand 0 1.75 +wire b_sign; // Sign of operand 1 1.76 +wire result_sign; // Sign of result 1.77 + 1.78 +///////////////////////////////////////////////////// 1.79 +// Instantiations 1.80 +///////////////////////////////////////////////////// 1.81 + 1.82 +lm32_addsub addsub ( 1.83 + // ----- Inputs ----- 1.84 + .DataA (operand_0_x), 1.85 + .DataB (operand_1_x), 1.86 + .Cin (adder_op_x), 1.87 + .Add_Sub (adder_op_x_n), 1.88 + // ----- Ouputs ----- 1.89 + .Result (adder_result_x), 1.90 + .Cout (adder_carry_n_x) 1.91 + ); 1.92 + 1.93 +///////////////////////////////////////////////////// 1.94 +// Combinational Logic 1.95 +///////////////////////////////////////////////////// 1.96 + 1.97 +// Extract signs of operands and result 1.98 + 1.99 +assign a_sign = operand_0_x[`LM32_WORD_WIDTH-1]; 1.100 +assign b_sign = operand_1_x[`LM32_WORD_WIDTH-1]; 1.101 +assign result_sign = adder_result_x[`LM32_WORD_WIDTH-1]; 1.102 + 1.103 +// Determine whether an overflow occured when performing a subtraction 1.104 + 1.105 +always @(*) 1.106 +begin 1.107 + // +ve - -ve = -ve -> overflow 1.108 + // -ve - +ve = +ve -> overflow 1.109 + if ( (!a_sign & b_sign & result_sign) 1.110 + || (a_sign & !b_sign & !result_sign) 1.111 + ) 1.112 + adder_overflow_x = `TRUE; 1.113 + else 1.114 + adder_overflow_x = `FALSE; 1.115 +end 1.116 + 1.117 +endmodule 1.118 +