`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: Alberto J. Molina Cantero
// 
// Create Date:    17:49:33 02/23/2012 
// Design Name: 
// Module Name:    SDP13 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module SDP7_estructural (input W, R, E, clk,dir);
	
	parameter n=8;

	wire [n-1:0] bus1,bus2;
	wire [n-1:0] channel [1:0];
	wire toL,toM;
	
	reg1	MBR (.clk(clk), .ent(bus1),.L(toL),.sal(bus2) );
	mux2a1 multiplexor (.canal0(channel[0]),.canal1(channel[1]),.sel(R),.sal(bus1)); 	
	RAM MEM (.EN(toM),.RW(R),.ent(bus2),.sal(channel[1]),.dir(dir)  );
	reg2 EXR (.sal(channel[0]) );

	assign	toL= R |E;
	assign 	toM = W | R;	
	
endmodule

module reg2 #(parameter n=8) (output [n-1:0] sal);
	reg [n-1:0] q;

	assign sal=q;

	initial q=5; //Para simulacin
	
endmodule

module reg1 #(parameter n=8) (input [n-1:0] ent, L, clk, output [n-1:0] sal);

	reg [n-1:0] q;
	
	always@(posedge clk)
		if(L)
			q<=ent;
			
	assign sal = q;

	initial q=10;  //Para simulacin
	
endmodule

module mux2a1 #(parameter n=8) (input [n-1:0] canal0,input [n-1:0] canal1 , sel, output reg [n-1:0]sal);
	always @*
		if(sel)
			sal = canal1;
		else
			sal = canal0;
endmodule


module RAM #(parameter k=10,  n=8)(input [k-1:0] dir, EN, RW, input [n-1:0] ent, output  [n-1:0] sal);
	
		reg [n-1:0] q [2**k -1:0];
		
		always @(*)
			if(EN)
				if (RW==0)
					q[dir] <= ent;
		
		assign sal = (EN*RW) ? q[dir] : 0;
		
		initial
			begin:iniram		//Debemos dar un nombre a un bloque cuando dentro de l se define una variable (int i)
				integer i;
				for(i=0;i<(2**k);i=i+1)
					q[i]=i%256;	/* Inicializacin de las posiciones de la memoria usando nmeros que se incrementan
							de forma progresiva. La direccin 0 con 0, la 1 con 1,.... la 255 con 255, la 256 con 0 y asi sucesivamente*/
			end
endmodule