Branch Misprediction Recovery in RISC-V - cpu-architecture

I'm now trying to implement pipeline CPU based on RV32I ISA.
My CPU has renaming algorithm for data hazards and branches prediction for control hazards.
This is my example code for renaming algorithm and branch prediction :
addi x1, x0, 25
addi x2, x0, 50
bne x1, x2, LABLE
addi x1, x0, 200
addi x2, x0, 300
LABLE:
addi x1, x1, 1
addi x2, x2, 1
Let me explains my CPU.
At line 1, "addi x1, x0, 25", x1's tag is 15.
At line 2, "addi x2, x0, 50", x2's tag is 14.
At line 3, "bne x1, x2, LABLE", the result of prediction is not taken. So, the instruction at line 4 is executed.
At line 4, "addi x1, x0, 200", x1's tag is 13. (This instruction should be not executed in true follow)
At line 5, "addi x2, x0, 300", x2's tag is 12. (This instruction should be not executed in true follow)
At line 6 and 7, these instructions need to read x1, x2 with the true tag is 15, 14, but in this case, the tags are read is 13, 12. So the CPU goes wrong.
So, when "bne" is done in the execute stage, How can it recover x1 and x2 tag to 15, 14 ?
What books or sources I should refer to resolve this problem ?

Related

How can i make this signal in MATLAB script to use in lsim command

i need to use this signal as the u of the lsim function. Since it's a really uneven signal i could only make it with a really messy code using ones and zeros for every interval and i can plot it like that with no problems but when i try to use the same data for t and u of the lsim function i get an error. Is there a different way to make this signal and use it with lsim.
This is the signal i need to use:
Signal
The code i used to plot this signal:
x1=0:.001:1.33;
x2=1.33:.001:2.33;
x3=2.33:.001:2.66;
x4=2.66:.001:3;
x5=3:.001:4.33;
x6=4.33:.001:5.33;
x7=5.33:.001:6;
x8=6:.001:7.33;
x9=7.33:.001:8.33;
x10=8.33:.001:9;
x11=9:.001:10;
y1=ones(size(x1));
y2=zeros(size(x2));
y3=ones(size(x3));
y4=zeros(size(x4));
y5=ones(size(x5));
y6=zeros(size(x6));
y7=ones(size(x7));
y8=zeros(size(x8));
y9=ones(size(x9));
y10=zeros(size(x10));
y11=ones(size(x11));
y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11];
x=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11];
plot(x,y)
axis([0 10 -0.3 1.5]);
The code for lsim function:
A=[-7 -14 -8;1 0 0;0 1 0];
B=[6;0;0];
C=[0 0 1];
D=[0];
t=x; //x from the code above
u=y; //y from the code above
x0=[0;0;0];
[y,x]=lsim(A,B,C,D,u,t,x0);
subplot(311);plot(t,x(:,1));
subplot(312);plot(t,x(:,2));
subplot(313);plot(t,x(:,3);``

Particular vector operation without loops

Given a vector x1, x2, ..., xN, I need to create a vector of (x_i + x_j) for i = 1,...,N, j = i+1,...,N.
E.g., for x1, x2, x3, x4:
x1+x2, x1+x3, x1+x4, x2+x3, x2+x4, x3+x4
How to do it without loops to get good performance?
C = combnk(v,k) returns a matrix containing all possible combinations of the elements of vector v taken k at a time.
So if you call
combnk(x,2)
you get
x3 x4
x2 x4
x2 x3
x1 x4
x1 x3
x1 x2
In case you rely on the order, which is now inverted, use flipud, then call sum
sum(flipud(combnk(x,2)),2)

Interpolation Errors, plot function does not generate proper lines when running

I am trying to get a graph made for my interpolation program (basically it takes a test function and creates lines utilizing an interpolation method)
clear all;
close all;
clc;
x1= [-1 0.75 -0.5 -0.25 0 0.25 0.5 0.75 1];
x2=-1:1001:1;
y1=1./(1+25.*x1.^2);
y2=1./(1+25.*x2.^2);
figure(777)
title('Figure 777')
for loop=1:1:6
subplot(6,2,loop);
plot(x2,y2,'linewidth',2);
hold on
plot(x1,y1,'r.','markersize',25);
hold on
end
%%%(A)
[p1,s1,u1] = polyfit(x1,y1,4);
Y1=polyval(p1, x2, s1, u1);
subplot (6,2,1)
plot(x2,Y1,'b','linewidth',6);
%%%(B)
[p2, s2, u2] = polyfit(x1,y1,8);
Y2 = polyval(p2, x2, s2, u2);
subplot(6,2,1)
plot(x2,Y2, 'b', 'linewidth', 6);
%%%(C)
Y3 = interp1(x1, y1, x2, 'linear');
subplot(6,3,1)
plot(x2, Y3, 'b', 'linewidth', 6);
%%%(D)
Y4 = interp1(x1,y1,x2, 'nearest');
subplot(6,4,1)
plot(x2, Y4, 'b', 'linewidth', 6);
%%%(E)
Y5 = interp1(x1,y1,x2, 'spline');
subplot(6,5,1)
plot(x2, Y5, 'b', 'linewidth', 6);
%%%(F)%%%(This method does not exist for Matlab R2017a)
Y6 = interp1(x1,y1,x2, 'makima');
subplot(6,6,1)
plot(x2, Y6, 'b', 'linewidth', 6);
However, none of the lines appear when I run the program. Only the dots. Furthermore I receive these errors:
Warning: Polynomial is badly conditioned. Add points with distinct X values or
reduce the degree of the polynomial.
In polyfit (line 73)
In HwSixTwo (line 27)
Error using griddedInterpolant
The grid vectors must contain unique points.
Error in interp1 (line 149)
F = griddedInterpolant(X,V,method);
Error in HwSixTwo (line 33)
Y3 = interp1(x1, y1, x2, 'linear');
Well one bug that would certainly not generate intended results is here:
x2=-1:1001:1;
I'm assuming it's your intent to create 1001 points evenly spaced between -1 to 1. This code is not doing this. The middle number specifies what the spacing is in between successive numbers. 1001 does not make sense which generates only one value which is -1.
Replace your code with the linspace function:
x2 = linspace(-1, 1, 1001);
Also, you're missing a minus sign in one of your values in x1 - the 0.75:
x1= [-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1];
% ^^^
As Cris noted, your x values must be monotonic or increasing / decreasing in one direction.

solve system of linear equations in matlab

I'm new to Matlab. Suppose I want to solve a linear system of 2 equations with 5 variables x1, x2, x3, x4, x5. Can Matlab give me solution for x1 and x2 in terms of the x3, x4, and x5? I also want to assign values to one or more variables, say I want to look at what happens if x3=5 or x3=3 and x5=1. Is there a way to achieve this?
I looked at the help page https://www.mathworks.com/help/symbolic/solve-a-system-of-linear-equations.html#d120e14359, but it does not cover the non-square matrix case
You can use multiple calls of solve to get solutions for x1 and x2. In this problem you can solve the first equation for x1, and then plug that into the second equation to get x2 in terms of x3, x4, and x5. You can then substitute the new value of x2 back into your solution of x1.
The subs function is used to substitute the solved values back into the original equation.
syms x1 x2 x3 x4 x5
eq1 = x1 + 4*x2 - 5*x3 + 2*x4 + x5;
eq2 = 3*x1 + 8*x2 - 3*x3 + x4 - x5;
x1s = solve(eq1, x1); % Solve x1 in term of x2-x5
x2s = solve(subs(eq2, x1, x1s), x2); % Solve x2 in terms of x3-x5
x1s = solve(subs(eq1, x2, x2s), x1); % Resolve x1 in terms of x3-x5
Output:
x1s =
3*x4 - 7*x3 + 3*x5
x2s =
3*x3 - (5*x4)/4 - x5
You can plug in values for x3, x4, and x5 using subs. For example, for x4=3 and x5=4:
subs(x1s, [x4 x5], [3 4])
ans =
21 - 7*x3

Symbolic executions over bit vectors

Is there any tool for bit vectors (QF_BV logic) which will symbolically execute the operations and return the outputs in terms of symbolic values of the bit vectors so that I can apply my own computations on them as needed? Or Is there any SMT solver where symbolic values of variables can be extracted after bit blasting? For example:
Let,
X[3:0], Y[3:0], Z[4:0], W[4:0] are declared as bit vectors without initializing any value
print X[3:0]
X[3:0] <- X[3:0] >> 1 (logical shift)
print X[3:0]
Z[4:0] <- X[3:0] + Y[3:0]
print Z[4:0]
print Y[3:0]
W[4:0] <- Y[3:0] + 0000
print W[4:0]
.......
Desired output (something symbolic like this):
2. [x3 x2 x1 x0]
4. [0 x3 x2 x1]
6. [s4 s3 s2 s1 s0]
7. [y3 y2 y1 y0]
9. [0 y3 y2 y1 y0]