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