appu1985
Member level 2
Code:
module pe3(clk,i,x,p,psw,psyi,mode,io,xo,po,pswo,psyo);
//Here evey where PE refers to the PROCESSING ELEMENT
parameter divfac=128; //The factor used to implement decimal multipication of learn rate and weight.
parameter size =1; //The Image size or the number of pixels in the image
//Input ports of the PROCESSING ELEMENT
input clk;
input [7:0]i; //Index of the input pixel
input [7:0]x; //Value of the pixel input
input [3:0]p; //The Index of the person whose image is being sent
input [7:0]psw; //Partial Sum for weight updating
input [15:0]psyi; //Partial Sum for output calculation
input [1:0]mode; //To select the operation as Recognition Phase or Training Phase 00,01- Recognition
//00- Projection Evaluation in Training Phase
//01- Weight Updation in Training Phase
//11 - Recognition Phase
//Output ports of the PROCESSING ELEMENT
output [7:0]io; //The index of the pixel which is evaluated at this PE and going to next PE
output [7:0]xo; //The value of pixel going to next PE
output [3:0]po; //The index value of the person whose image is under processing
output [7:0]pswo; //The calculated the Partial Sum of Weight update sent to next PE as well as stored here
output [15:0]psyo;
//Register Declarations inside the PROCESSING ELEMENT
reg [7:0]w [size-1:0]; //A register in the processig element to store the weights of the Image
reg [15:0]ylearn [15:0]; //A register to store the projection "Y" of each image max 16 images
reg [8:0]psw1; //A register to store the partial sum evaluated
reg [15:0]pso1; //A register to store the Partial Sum for evaluating the output projection " Y ".
reg [15:0]count; //A register which counts the number of pixels entered the PE and counts which helps in evaluating the Projections
reg [7:0]t; //A register used to evaluate the Partial Sum during Weight updation
reg [15:0]yrcog; //A register which stores the projections during the recognition phase of the new image
reg [15:0]rectmp;
wire [15:0]psyo; //A register which accumulates the partial sum before the projection is evaluated
//Wire Declaration as used inside the processing element
wire [7:0]w1; //This returns the updated weight from the Out22 module and is stored in W reg.
wire [15:0]psj; //This returns the Partial Sum for projection evaluation from the Out11 module
wire [7:0]pswo;
integer l;
out22 s2(clk,w[i],ylearn[p],x,psw1,w1); //Module which is used for Weight Updation
out11 s1(clk,x,w[i],psy,psj); //module which is used for evaluating the projections
initial
begin
w[1] = 8'b00000010;
ylearn[1] = 16'b0000000000000010;
pso1 = 16'b0000000000000001;
end
//First Mode of Operation when the projections are to be evaluated.
always @(posedge clk) // MODE - OO coressponds to PROJECTION EVALUATION STAGE
begin
count <= count + 1 ; //Counter is incremented till the image end is reached
if(mode == 2'b00 ) //It checks if the mode of operation is for Output evaluation
begin
pso1 <= pso1 + psj; //Keeps on accumulating the Partial Sum for Output evaluation
if(count==size) //If one image has passed then it starts assigning the outputs . size is a parmameter holding the image size.
begin //This is only after N clock pulses if the number of pixels in the image were N
ylearn[p] <= psj; //The Output is assigned to Ylearn the respective projection
end
end
//Once all the outputs are evaluated the mode is changed to 00 or 01 to Update the weights.
if(mode == 2'b01) //MODE - 01 corresponds to evaluating and updating weights.
begin
t <= (w[i]*ylearn[p]);//divfac; // t register is assigned the partial sum which is divided by divfac = 128 to enable the decimal arithmatic operations
psw1 <= psw + t; //PSW1 accumulates the partial sum of the weight updation formula
//Now for the jth PE if the PS has been evaluattes
w[i] <= w1; //The new updated weight from the Out2 module comes as w1 which is assigned to w[i].
// Assigns the value of psw1 to psw whenevr it changes.
end
//Now we shall operate for the recognition phase.
//MODE - 11 Corresponds to last phase of Recognition phase which implies a new input image.
if(mode == 2'b11)
begin
rectmp <=rectmp + (x * (w[i]));//divfac));//The register rectemp in each PE evaluated the projection and stores them in the register.
if(i == size) //When the entire image has passed through a processign element the projection has been computed and is assigned to a separate Register "yrcog".
begin //This is only after N clock pulses if the number of pixels in the image were N
yrcog <= rectmp;
end
end
end
//Recognition Phase ends here
assign pswo = psw1;
assign psyo = (psyi - yrcog); //Now the inputs to this PE are to be transferred to next one.
assign io = i; //The index of the input pixel is passed onto the next PE.
assign xo = x; //The pixel (value) processed by this PE is passed to next for its processing.
assign po = p; //The image index (Which number of the image is it.?) is also passed on to the next one.
endmodul
In the above code out11 and out22 are working fine now when i supply i=1,mode=01,x =1 ,size =1,w[1]=2,psw =2 then pswo should appear in the simulation output. pls verify