// Via http://www.edaplayground.com/s/6/591 // stackoverflow 20556634 // http://stackoverflow.com/questions/20556634/how-can-i-iteratively-create-buses-of-parameterized-size-to-connect-modules-also // Code your design here `define macro_args `define indexed_part_select module Multiplier_flat #(parameter M = 4, parameter N = 4)( input [M-1:0] A, //Input A, size M input [N-1:0] B, //Input B, size N output [M+N-1:0] P ); //Output P (product), size M+N /* Calculate LSB using Wolfram Alpha N==3 : http://www.wolframalpha.com/input/?i=0%2C+4%2C+9%2C+15%2C+... N==4 : http://www.wolframalpha.com/input/?i=0%2C+5%2C+11%2C+18%2C+26%2C+35%2C+... N==5 : http://www.wolframalpha.com/input/?i=0%2C+6%2C+13%2C+21%2C+30%2C+... */ `ifdef macro_args // initial $display("Use Macro Args"); `define calc_pp_lsb(n) (((n)-1)*((n)+2*M)/2) //`define calc_pp_msb(n) (`calc_pp_lsb(n+1)-1) `define calc_pp_msb(n) ((n**2+(2*M+1)*n-2)/2) //`define calc_range(n) `calc_pp_msb(n):`calc_pp_lsb(n) `define calc_pp_range(n) `calc_pp_lsb(n) +: (M+n) wire [`calc_pp_msb(N):0] PP; assign PP[`calc_pp_range(1)] = { 1'b0 , { A & {M{B[0]}} } }; assign P = PP[`calc_pp_range(N)]; `elsif indexed_part_select // initial $display("Use Indexed Part Select"); localparam MSB = (N**2+(2*M+1)*N-2)/2; wire [MSB:0] PP; assign PP[M:0] = { 1'b0 , { A & {M{B[0]}} } }; assign P = PP[MSB -: M+N]; `else // initial $display("Use Worst Case"); localparam MSB = (N**2+(2*M+1)*N-2)/2; wire [MSB:0] PP; assign PP[M:0] = { 1'b0 , { A & {M{B[0]}} } }; assign P = PP[MSB : MSB+1-M-N]; `endif genvar i; generate for (i=1; i < N; i=i+1) begin: addPartialProduct wire [M+i-1:0] gA,gB,gS; wire Cout; assign gA = { A & {M{B[i]}} , {i{1'b0}} }; `ifdef macro_args assign gB = PP[`calc_pp_range(i)]; assign PP[`calc_pp_range(i+1)] = {Cout,gS}; `elsif indexed_part_select assign gB = PP[(i-1)*(i+2*M)/2 +: M+i]; assign PP[i*((i+1)+2*M)/2 +: M+i+1] = {Cout,gS}; `else localparam LSB = (i-1)*(i+2*M)/2; localparam MSB = (i**2+(2*M+1)*i-2)/2; localparam MSB2 = ((i+1)**2+(2*M+1)*(i+1)-2)/2; assign gB = PP[MSB : LSB]; assign PP[ MSB2 : MSB+1] = {Cout,gS}; `endif RippleCarryAdder#(M+i) adder( .A(gA), .B(gB), .S(gS), .Cin (1'b0), .Cout(Cout) ); end endgenerate `ifdef macro_args // Cleanup global space `undef calc_pp_range `undef calc_pp_msb `undef calc_pp_lsb `endif endmodule module Multiplier_2D #(parameter M = 4, parameter N = 4)( input [M-1:0] A, //Input A, size M input [N-1:0] B, //Input B, size N output [M+N-1:0] P ); //Output P (product), size M+N wire [M+N-1:0] PP [N-1:0]; // Note: bits PP[0][M+N-1:M+1] are never used. Unused bits are optimized out during synthesis //assign PP[0][M:0] = { {1'b0} , { A & {M{B[0]}} } }; //assign PP[0][M+N-1:M+1] = {(N-1){1'b0}}; // uncomment to make probing readable assign PP[0] = { {N{1'b0}} , { A & {M{B[0]}} } }; assign P = PP[N-1]; genvar i; generate for (i=1; i < N; i=i+1) begin: addPartialProduct wire [M+i-1:0] gA,gB,gS; wire Cout; assign gA = { A & {M{B[i]}} , {i{1'b0}} }; assign gB = PP[i-1][M+i-1:0]; //assign PP[i][M+i:0] = {Cout,gS}; //if (i+1 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*
             LUFA Library
     Copyright (C) Dean Camera, 2012.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Header file for SCSI.c.
 */

#ifndef _SCSI_H_
#define _SCSI_H_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/pgmspace.h>

		#include <LUFA/Drivers/USB/USB.h>

		#include "../TempDataLogger.h"
		#include "../Descriptors.h"
		#include "DataflashManager.h"
		#include "Config/AppConfig.h"

	/* Macros: */
		/** Macro to set the current SCSI sense data to the given key, additional sense code and additional sense qualifier. This
		 *  is for convenience, as it allows for all three sense values (returned upon request to the host to give information about
		 *  the last command failure) in a quick and easy manner.
		 *
		 *  \param[in] Key    New SCSI sense key to set the sense code to
		 *  \param[in] Acode  New SCSI additional sense key to set the additional sense code to
		 *  \param[in] Aqual  New SCSI additional sense key qualifier to set the additional sense qualifier code to
		 */
		#define SCSI_SET_SENSE(Key, Acode, Aqual)  MACROS{ SenseData.SenseKey                 = (Key);   \
		                                                   SenseData.AdditionalSenseCode      = (Acode); \
		                                                   SenseData.AdditionalSenseQualifier = (Aqual); }MACROE

		/** Macro for the \ref SCSI_Command_ReadWrite_10() function, to indicate that data is to be read from the storage medium. */
		#define DATA_READ           true

		/** Macro for the \ref SCSI_Command_ReadWrite_10() function, to indicate that data is to be written to the storage medium. */
		#define DATA_WRITE          false

		/** Value for the DeviceType entry in the SCSI_Inquiry_Response_t enum, indicating a Block Media device. */
		#define DEVICE_TYPE_BLOCK   0x00

		/** Value for the DeviceType entry in the SCSI_Inquiry_Response_t enum, indicating a CD-ROM device. */
		#define DEVICE_TYPE_CDROM   0x05

	/* Function Prototypes: */
		bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);

		#if defined(INCLUDE_FROM_SCSI_C)
			static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);
			static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);
			static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);
			static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);
			static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
			                                      const bool IsDataRead);
			static bool SCSI_Command_ModeSense_6(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo);
		#endif

#endif