I've been trying to simulate a JK-FF with gate level code, but it's not working. Any help is appreciated.
Circuit code:
module circuit1_3_c(j,k,r,cp,q,q1);
input j,k,r,cp;
output q,q1;
wire t1,t2,t3,t4;
nand(t1,t2,q);
nand(t2,t1,j,cp,t3);
nand(t3,cp,k,t4);
nand(t4,t3,t1);
nand(q,q1,t2);
nand(q1,q,t3,r);
endmodule
Testbench code:
module circuit1_3_ctest;
parameter STEP=10;
parameter HALF_STEP=5;
reg j,k,r,cp;
wire q,q1;
circuit1_3_c circ(j,k,r,cp,q,q1);
initial begin
$dumpfile("circuit1_3_c.vcd");
$dumpvars(0,circuit1_3_ctest);
$monitor("\%t: J=%b, K=%b, R=%b, Cp=%b, Q=%b, Qbar=%b", $time, j,k,r,cp,q,q1);
r<=1'b1;
cp<=1'b0;
j<=1'b0; k<=1'b0; r<=1'b1;
#STEP;
j<=1'b0; k<=1'b1; r<=1'b1;
#STEP;
j<=1'b1; k<=1'b0; r<=1'b1;
#STEP;
j<=1'b1; k<=1'b1; r<=1'b1;
#STEP;
j<=1'b0; k<=1'b0; r<=1'b1;
#STEP;
j<=1'b0; k<=1'b1; r<=1'b1;
#STEP;
j<=1'b1; k<=1'b0; r<=1'b1;
#STEP;
j<=1'b1; k<=1'b1; r<=1'b1;
#HALF_STEP
$finish;
end
always #HALF_STEP cp=~cp;
endmodule
Output. As you can see, q and q1 are always unknown (x):
[1
You need to reset your logic properly. One way is to drive r as 0 at time 0.
Change:
r<=1'b1;
cp<=1'b0;
j<=1'b0; k<=1'b0; r<=1'b1;
to:
cp<=1'b0;
j<=1'b0; k<=1'b0; r<=1'b0;
Output:
0: J=0, K=0, R=0, Cp=0, Q=0, Qbar=1
5: J=0, K=0, R=0, Cp=1, Q=0, Qbar=1
10: J=0, K=1, R=1, Cp=0, Q=0, Qbar=1
15: J=0, K=1, R=1, Cp=1, Q=0, Qbar=1
20: J=1, K=0, R=1, Cp=0, Q=0, Qbar=1
25: J=1, K=0, R=1, Cp=1, Q=1, Qbar=0
30: J=1, K=1, R=1, Cp=0, Q=1, Qbar=0
35: J=1, K=1, R=1, Cp=1, Q=0, Qbar=1
40: J=0, K=0, R=1, Cp=0, Q=0, Qbar=1
45: J=0, K=0, R=1, Cp=1, Q=0, Qbar=1
50: J=0, K=1, R=1, Cp=0, Q=0, Qbar=1
55: J=0, K=1, R=1, Cp=1, Q=0, Qbar=1
60: J=1, K=0, R=1, Cp=0, Q=0, Qbar=1
65: J=1, K=0, R=1, Cp=1, Q=1, Qbar=0
70: J=1, K=1, R=1, Cp=0, Q=1, Qbar=0
Related
I am writing some eBPF programs in Rust using redBPF and I've encountered some issue with the verifier that only appears on some kernels.
This is a minimal reproducer XDP probe that shows the issue:
#[xdp]
unsafe fn xdp_test(ctx: XdpContext) -> XdpResult {
let data = ctx.data()?;
let start = ctx.data_start();
let off = data.offset();
let end = ctx.data_end();
/* Ensuring an upper bound for off doesn't make any difference
if off > 50 {
return XdpResult::Err(OutOfBounds);
}
*/
let mut address = start + off;
for i in 0..500 {
address = start + off + i;
if address <= start || address >= end {
break;
}
// This line (packet access) fails on kernel 5.10, but works fine on 5.13
let byte = *(address as *const u8);
// Just so the packet read above doesn't get optimized away
printk!("%u", byte as u32);
}
Ok(XdpAction::Pass)
}
Compiling this into eBPF bytecode and loading it into an Ubuntu 5.13 kernel (5.13.0-48-generic #54~20.04.1-Ubuntu SMP Thu Jun 2 23:37:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux) works without issues. The verifier accepts the program.
However, trying to load the exact same bytecode into an Android emulator kernel 5.10.66-android12-9-00041-gfa9c9074531e-ab7914766 #1 SMP PREEMPT Fri Nov 12 11:36:25 UTC 2021 x86_64 the verifier rejects the program. This is the full error log from the verifier:
ret=-1 os error=Permission denied (os error 13): 0: (61) r6 = *(u32 *)(r1 +4)
1: (61) r7 = *(u32 *)(r1 +0)
2: (bf) r1 = r7
3: (07) r1 += 14
4: (2d) if r1 > r6 goto pc+43
R1_w=pkt(id=0,off=14,r=14,imm=0) R6_w=pkt_end(id=0,off=0,imm=0) R7_w=pkt(id=0,off=0,r=14,imm=0) R10=fp0
5: (71) r2 = *(u8 *)(r7 +13)
6: (67) r2 <<= 8
7: (71) r3 = *(u8 *)(r7 +12)
8: (4f) r2 |= r3
9: (55) if r2 != 0x8 goto pc+38
R1_w=pkt(id=0,off=14,r=14,imm=0) R2_w=inv8 R3_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R6_w=pkt_end(id=0,off=0,imm=0) R7_w=pkt(id=0,off=0,r=14,imm=0) R10=fp0
10: (bf) r2 = r7
11: (07) r2 += 34
12: (2d) if r2 > r6 goto pc+35
R1=pkt(id=0,off=14,r=34,imm=0) R2=pkt(id=0,off=34,r=34,imm=0) R3=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
13: (71) r3 = *(u8 *)(r1 +0)
14: (67) r3 <<= 2
15: (57) r3 &= 60
16: (71) r2 = *(u8 *)(r1 +9)
17: (0f) r1 += r3
last_idx 17 first_idx 12
regs=8 stack=0 before 16: (71) r2 = *(u8 *)(r1 +9)
regs=8 stack=0 before 15: (57) r3 &= 60
regs=8 stack=0 before 14: (67) r3 <<= 2
regs=8 stack=0 before 13: (71) r3 = *(u8 *)(r1 +0)
18: (15) if r2 == 0x11 goto pc+31
R1_w=pkt(id=1,off=14,r=0,umax_value=60,var_off=(0x0; 0x3c)) R2_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R3_w=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
19: (55) if r2 != 0x6 goto pc+28
R1_w=pkt(id=1,off=14,r=0,umax_value=60,var_off=(0x0; 0x3c)) R2_w=inv6 R3_w=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
20: (2d) if r7 > r1 goto pc+27
R1=pkt(id=1,off=14,r=0,umax_value=60,var_off=(0x0; 0x3c)) R2=inv6 R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
21: (bf) r2 = r1
22: (07) r2 += 20
23: (2d) if r2 > r6 goto pc+24
R1=pkt(id=1,off=14,r=34,umax_value=60,var_off=(0x0; 0x3c)) R2_w=pkt(id=1,off=34,r=34,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
24: (71) r2 = *(u8 *)(r1 +12)
25: (77) r2 >>= 2
26: (57) r2 &= 60
27: (0f) r1 += r2
last_idx 27 first_idx 20
regs=4 stack=0 before 26: (57) r2 &= 60
regs=4 stack=0 before 25: (77) r2 >>= 2
regs=4 stack=0 before 24: (71) r2 = *(u8 *)(r1 +12)
28: (2d) if r7 > r1 goto pc+19
R1=pkt(id=2,off=14,r=0,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
29: (bf) r8 = r1
30: (3d) if r1 >= r6 goto pc+17
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8_w=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R10=fp0
31: (bf) r1 = r8
32: (1f) r1 -= r7
33: (25) if r1 > 0x32 goto pc+14
R1_w=inv(id=0,umax_value=50,var_off=(0x0; 0xffffffff)) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8_w=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R10=fp0
34: (b7) r9 = 0
35: (bf) r1 = r8
36: (0f) r1 += r9
last_idx 36 first_idx 28
regs=200 stack=0 before 35: (bf) r1 = r8
regs=200 stack=0 before 34: (b7) r9 = 0
37: (3d) if r7 >= r1 goto pc+10
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R9=invP0 R10=fp0
38: (3d) if r1 >= r6 goto pc+9
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R9=invP0 R10=fp0
39: (71) r3 = *(u8 *)(r1 +0)
invalid access to packet, off=14 size=1, R1(id=2,off=14,r=13)
R1 offset is outside of the packet
processed 40 insns (limit 1000000) max_states_per_insn 0 total_states 4 peak_states 4 mark_read 2
As I understand the issue, the verifier of the 5.10 kernel rejects the dereferencing of the packet pointer, claiming that we don't have validated that it is always within bounds (reading at offset 14 while r is 13). We do in fact check this just above.
Interestingly, if I oversize the bounds check above to something like this, both the 5.10 and 5.13 kernel verifiers accept the program:
[snip]
for i in 0..500 {
address = start + off + i;
// Checking 2 bytes ahead makes 5.10 verifier happy
if address <= start || (address + 2) >= end {
break;
}
// Works on both 5.10 and 5.13
let byte = *(address as *const u8);
// Just so the packet read above doesn't get optimized away
printk!("%u", byte as u32);
}
Ok(XdpAction::Pass)
}
But the above is not what I want, because this causes the bounded loop to abort too early - I want the loop to run fully, if the packet is large enough. I have tried the usual tricks I do when I run into verifier issues, but so far to no avail. I don't quite understand why the 5.10 verifier is unhappy with the first example. Usually this is related to some unbounded registers, but as far as I can see all bounds should be satisfied.
I have tried looking at a diff of the kernel verifier between the two versions, but couldn't see any obvious change that causes this.
TL;DR. You are missing bug fix 2fa7d94afc1a for the BPF verifier. It was backported to the v5.13 kernel you are using as commit e7a61f15beea, but not to the v5.10 kernel.
You might want to try a newer Android kernel if possible, or to ask them to carry the bugfix if they don't on v5.10.
Verifier Error Explanation
I removed parts of the output that were irrelevant here.
R1=pkt(id=2,off=14,r=0,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R10=fp0
29: (bf) r8 = r1
30: (3d) if r1 >= r6 goto pc+17
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8_w=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R10=fp0
[...]
34: (b7) r9 = 0
35: (bf) r1 = r8
36: (0f) r1 += r9
37: (3d) if r7 >= r1 goto pc+10
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R9=invP0 R10=fp0
38: (3d) if r1 >= r6 goto pc+9
R1=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R2=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R3=invP(id=0,umax_value=60,var_off=(0x0; 0x3c)) R6=pkt_end(id=0,off=0,imm=0) R7=pkt(id=0,off=0,r=34,imm=0) R8=pkt(id=2,off=14,r=13,umax_value=120,var_off=(0x0; 0x7c),s32_max_value=124,u32_max_value=124) R9=invP0 R10=fp0
39: (71) r3 = *(u8 *)(r1 +0)
invalid access to packet, off=14 size=1, R1(id=2,off=14,r=13)
R1 offset is outside of the packet
The verifier tells us that we are trying to access the packet at offset 14 (off=14) with an access size of 1 byte when the packet is only known to be at least 13 bytes long (r=13). The known packet length here is incorrect somehow because you checked that address >= end.
Going up, we can check where that packet range (r=13) is coming from. It is assigned from R8 at instruction 35, itself assigned from R1 at instruction 29.
At instruction 30, we find the address >= end check in its bytecode form. We see that both R1 and R8's ranges are updated from r=0 to r=13 after this check. That is however incorrect and should be updated to off+1, hence r=15.
Checking the Verifier Sources
In the verifier, this update of the range is implemented in find_good_pkt_pointers. The update logic looks fine and shouldn't cause this issue.
But if we git blame those lines, we can see they were changed in commit 2fa7d94afc1a. The commit describes the off-by-two error you are hitting:
This commit fixes the off-by-two error by adjusting new_range in the
right direction and fixes the tests by changing the range into the
one that should actually fail.
This commit was released upstream in v5.16. Checking the Ubuntu sources for Ubuntu-hwe-5.13-5.13.0-48.54_20.04.1, we find this same commit backported as e7a61f15beea, which explains why it works on your v5.13 kernel.
PyVista is great, and let's me do lots of cool things very conveniently. I would like to cast the shadow of a 3D object onto a plane with grid lines. Here's what I would like, minus the shadows, shadows=False, gridlines=True
I can enable shadows without the grid lines: gridlines=False, shadows=True
but when I try to do both gridlines=True, shadows=True I get a massive error trace as per below and a result just like above, shadows but no grid lines.
QUESTION: is what I am trying to possible? Am I misunderstanding grids and shadows, have I coded it wrong, or is this a bug. My bigger program, with more graphical entities will segfault. I am running PyVista 0.29.0, Python 3.8.5 on MacOS 10.15.7. Similar problems arise if I try to cast the shadow on to a StructuredGrid.
Below is my minimal code example
import pyvista as pv
import vtk
import numpy as np
# change these values
gridlines = True
shadows = False
plotter = pv.Plotter(polygon_smoothing=True, window_size=(2000,2000))
plotter.disable_parallel_projection()
sphere = pv.Sphere(radius=0.3, center=(0,0,1))
plotter.add_mesh(sphere, ambient=0.2, diffuse=0.5, specular=0.8, specular_power=30,
smooth_shading=True, color='dodgerblue')
# add the grid
grid = pv.Plane(i_size=5, j_size=5)
plotter.add_mesh(grid, show_edges=gridlines, ambient=0, diffuse=0.5, specular=0.8, color='red', edge_color='white')
if shadows:
# do the shadows
shadows = vtk.vtkShadowMapPass()
seq = vtk.vtkSequencePass()
passes = vtk.vtkRenderPassCollection()
passes.AddItem(shadows.GetShadowMapBakerPass())
passes.AddItem(shadows)
seq.SetPasses(passes)
# Tell the renderer to use our render pass pipeline
cameraP = vtk.vtkCameraPass()
cameraP.SetDelegatePass(seq)
plotter.renderer.SetPass(cameraP)
plotter.set_background('white')
plotter.show()
Error trace:
(dev) [233 ~...code/RVC3-python/tools] % /Users/corkep/opt/miniconda3/envs/dev/bin/python /Users/corkep/Dropbox/code/RVC3-python/chapter8/pvbug.py
2021-03-11 19:57:38.908 ( 0.621s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 ( 0.622s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 ( 0.622s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 ( 0.622s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 ( 0.622s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.990 ( 0.704s) [ 84DF2C] vtkShaderProgram.cxx:452 ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifdef GL_ES
3: #ifdef GL_FRAGMENT_PRECISION_HIGH
4: precision highp float;
5: precision highp sampler2D;
6: precision highp sampler3D;
7: #else
8: precision mediump float;
9: precision mediump sampler2D;
10: precision mediump sampler3D;
11: #endif
12: #define texelFetchBuffer texelFetch
13: #define texture1D texture
14: #define texture2D texture
15: #define texture3D texture
16: #else // GL_ES
17: #define highp
18: #define mediump
19: #define lowp
20: #if __VERSION__ == 150
21: #define texelFetchBuffer texelFetch
22: #define texture1D texture
23: #define texture2D texture
24: #define texture3D texture
25: #endif
26: #endif // GL_ES
27: #define varying in
28:
29:
30: /*=========================================================================
31:
32: Program: Visualization Toolkit
33: Module: vtkPolyDataFS.glsl
34:
35: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
36: All rights reserved.
37: See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
38:
39: This software is distributed WITHOUT ANY WARRANTY; without even
40: the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
41: PURPOSE. See the above copyright notice for more information.
42:
43: =========================================================================*/
44: // Template for the polydata mappers fragment shader
45:
46: uniform int PrimitiveIDOffset;
47:
48:
49:
50: // VC position of this fragment
51: //VTK::PositionVC::Dec
52:
53: // Camera prop
54: uniform int cameraParallel;
55:
56:
57: // optional color passed in from the vertex shader, vertexColor
58: uniform float ambientIntensity; // the material ambient
59: uniform float diffuseIntensity; // the material diffuse
60: uniform float opacityUniform; // the fragment opacity
61: uniform vec3 ambientColorUniform; // ambient color
62: uniform vec3 diffuseColorUniform; // diffuse color
63:
64:
65: // optional surface normal declaration
66: //VTK::Normal::Dec
67:
68: // extra lighting parameters
69: uniform vec3 lightColor0;
70: uniform vec3 lightDirectionVC0; // normalized
71: uniform vec3 lightColor1;
72: uniform vec3 lightDirectionVC1; // normalized
73: uniform vec3 lightColor2;
74: uniform vec3 lightDirectionVC2; // normalized
75: uniform vec3 lightColor3;
76: uniform vec3 lightDirectionVC3; // normalized
77: uniform vec3 lightColor4;
78: uniform vec3 lightDirectionVC4; // normalized
79:
80: uniform float depthC;
81: vec2 calcShadow(in vec4 vert,
82: in sampler2D shadowMap,
83: in mat4 shadowTransform,
84: in float attenuation,
85: in int shadowParallel,
86: in float sNearZ, in float sFarZ)
87: {
88: vec4 shadowCoord = shadowTransform*vert;
89: float expFactor = 8.0;
90: float thickness = 0.0;
91: if(shadowCoord.w > 0.0)
92: {
93: vec2 projected = shadowCoord.xy/shadowCoord.w;
94: if(projected.x >= 0.0 && projected.x <= 1.0
95: && projected.y >= 0.0 && projected.y <= 1.0)
96: {
97: float ldepth = shadowCoord.z;
98: if (shadowParallel == 0) { ldepth = (shadowCoord.w - sNearZ)/(sFarZ - sNearZ); }
99: float depthCExpActual = exp(- depthC*ldepth);
100: float depthCExpBlured = texture2D(shadowMap,projected).r;
101: expFactor = depthCExpBlured * depthCExpActual;
102: float depth = log(depthCExpBlured)/depthC;
103: thickness = clamp(ldepth - depth, 0.0, 1.0)*(sFarZ - sNearZ);
104: if (expFactor > 1.0) { expFactor = 1.0; }
105: }
106: }
107: return vec2(1.0 - attenuation + attenuation*expFactor, thickness);
108: }
109: uniform int shadowParallel0;
110: uniform float shadowNearZ0;
111: uniform float shadowFarZ0;
112: uniform float shadowAttenuation0;
113: uniform sampler2D shadowMap0;
114: uniform mat4 shadowTransform0;
115: uniform int shadowParallel1;
116: uniform float shadowNearZ1;
117: uniform float shadowFarZ1;
118: uniform float shadowAttenuation1;
119: uniform sampler2D shadowMap1;
120: uniform mat4 shadowTransform1;
121: uniform int shadowParallel2;
122: uniform float shadowNearZ2;
123: uniform float shadowFarZ2;
124: uniform float shadowAttenuation2;
125: uniform sampler2D shadowMap2;
126: uniform mat4 shadowTransform2;
127: uniform int shadowParallel3;
128: uniform float shadowNearZ3;
129: uniform float shadowFarZ3;
130: uniform float shadowAttenuation3;
131: uniform sampler2D shadowMap3;
132: uniform mat4 shadowTransform3;
133:
134:
135: // Texture maps
136: //VTK::TMap::Dec
137:
138: // Texture coordinates
139: //VTK::TCoord::Dec
140:
141: // picking support
142: //VTK::Picking::Dec
143:
144: // Depth Peeling Support
145: //VTK::DepthPeeling::Dec
146:
147: // clipping plane vars
148: //VTK::Clip::Dec
149:
150: // the output of this shader
151: out vec4 fragOutput0;
152:
153:
154: // Apple Bug
155: //VTK::PrimID::Dec
156:
157: // handle coincident offsets
158: uniform float cCValue;
159: uniform float cSValue;
160: uniform float cDValue;
161:
162: // Value raster
163: //VTK::ValuePass::Dec
164:
165: void main()
166: {
167: // VC position of this fragment. This should not branch/return/discard.
168: //VTK::PositionVC::Impl
169:
170: // Place any calls that require uniform flow (e.g. dFdx) here.
171: //VTK::UniformFlow::Impl
172:
173: // Set gl_FragDepth here (gl_FragCoord.z by default)
174: float Zdc = gl_FragCoord.z*2.0 - 1.0;
175: float Z2 = -1.0*cDValue/(Zdc + cCValue) + cSValue;
176: float Zdc2 = -1.0*cCValue - cDValue/Z2;
177: gl_FragDepth = Zdc2*0.5 + 0.5;
178:
179:
180: // Early depth peeling abort:
181: //VTK::DepthPeeling::PreColor
182:
183: // Apple Bug
184: //VTK::PrimID::Impl
185:
186: //VTK::Clip::Impl
187:
188: //VTK::ValuePass::Impl
189:
190: vec3 ambientColor = ambientIntensity * ambientColorUniform;
191: vec3 diffuseColor = diffuseIntensity * diffuseColorUniform;
192: float opacity = opacityUniform;
193:
194:
195: // Generate the normal if we are not passed in one
196: //VTK::Normal::Impl
197:
198: vec2 factor0 = vec2(1.0);
199: vec2 factor1 = calcShadow(vertexVC, shadowMap0, shadowTransform0, shadowAttenuation0, shadowParallel0, shadowNearZ0, shadowFarZ0);
200: vec2 factor2 = calcShadow(vertexVC, shadowMap1, shadowTransform1, shadowAttenuation1, shadowParallel1, shadowNearZ1, shadowFarZ1);
201: vec2 factor3 = calcShadow(vertexVC, shadowMap2, shadowTransform2, shadowAttenuation2, shadowParallel2, shadowNearZ2, shadowFarZ2);
202: vec2 factor4 = calcShadow(vertexVC, shadowMap3, shadowTransform3, shadowAttenuation3, shadowParallel3, shadowNearZ3, shadowFarZ3);
203: fragOutput0 = vec4(ambientColor + diffuseColor, opacity);
204: //VTK::Light::Impl
205:
206:
207:
208: //VTK::TCoord::Impl
209:
210: if (fragOutput0.a <= 0.0)
211: {
212: discard;
213: }
214:
215: //VTK::DepthPeeling::Impl
216:
217: //VTK::Picking::Impl
218:
219: // handle coincident offsets
220: //VTK::Coincident::Impl
221: }
ERROR:root:1: #version 150
2021-03-11 19:57:38.991 ( 0.705s) [ 84DF2C] vtkShaderProgram.cxx:453 ERR| vtkShaderProgram (0x7fee29ba49b0): ERROR: 0:199: Use of undeclared identifier 'vertexVC'
ERROR: 0:200: Use of undeclared identifier 'vertexVC'
ERROR: 0:201: Use of undeclared identifier 'vertexVC'
ERROR: 0:202: Use of undeclared identifier 'vertexVC'
ERROR:root:ERROR: 0:199: Use of undeclared identifier 'vertexVC'
2021-03-11 19:57:39.023 ( 0.737s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 ( 0.737s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 ( 0.737s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 ( 0.737s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 ( 0.737s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.024 ( 0.738s) [ 84DF2C] vtkShaderProgram.cxx:437 ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifndef GL_ES
3: #define highp
4: #define mediump
5: #define lowp
6: #endif // GL_ES
7: #define attribute in
8: #define varying out
9:
10:
11: /*=========================================================================
12:
13: Program: Visualization Toolkit
14: Module: vtkPolyDataVS.glsl
15:
16: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
17: All rights reserved.
18: See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
19:
20: This software is distributed WITHOUT ANY WARRANTY; without even
21: the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22: PURPOSE. See the above copyright notice for more information.
23:
24: =========================================================================*/
25:
26: in vec4 vertexMC;
27:
28:
29:
30: // frag position in VC
31: //VTK::PositionVC::Dec
32:
33: // optional normal declaration
34: //VTK::Normal::Dec
35:
36: // extra lighting parameters
37: //VTK::Light::Dec
38:
39: // Texture coordinates
40: //VTK::TCoord::Dec
41:
42: // material property values
43: //VTK::Color::Dec
44:
45: // clipping plane vars
46: //VTK::Clip::Dec
47:
48: // camera and actor matrix values
49: uniform mat4 MCDCMatrix;
50:
51: // Apple Bug
52: //VTK::PrimID::Dec
53:
54: // Value raster
55: //VTK::ValuePass::Dec
56:
57: // picking support
58: //VTK::Picking::Dec
59:
60: void main()
61: {
62: //VTK::Color::Impl
63:
64: //VTK::Normal::Impl
65:
66: //VTK::TCoord::Impl
67:
68: //VTK::Clip::Impl
69:
70: //VTK::PrimID::Impl
71:
72: gl_Position = MCDCMatrix * vertexMC;
73:
74:
75: //VTK::ValuePass::Impl
76:
77: //VTK::Light::Impl
78:
79: //VTK::Picking::Impl
80: }
ERROR:root:1: #version 150
2021-03-11 19:57:39.025 ( 0.739s) [ 84DF2C] vtkShaderProgram.cxx:438 ERR| vtkShaderProgram (0x7fee29ba49b0):
WARNING:root:ERROR: In /Users/tjcorona/Development/vtk/source/Rendering/OpenGL2/vtkShaderProgram.cxx, line 438
vtkShaderProgram (0x7fee29ba49b0):
2021-03-11 19:57:39.292 ( 1.006s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 ( 1.006s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 ( 1.006s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 ( 1.006s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 ( 1.006s) [ 84DF2C] vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.293 ( 1.007s) [ 84DF2C] vtkShaderProgram.cxx:437 ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifndef GL_ES
3: #define highp
4: #define mediump
5: #define lowp
6: #endif // GL_ES
7: #define attribute in
8: #define varying out
9:
10:
11: /*=========================================================================
12:
13: Program: Visualization Toolkit
14: Module: vtkPolyDataVS.glsl
15:
16: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
17: All rights reserved.
18: See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
19:
20: This software is distributed WITHOUT ANY WARRANTY; without even
21: the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22: PURPOSE. See the above copyright notice for more information.
23:
24: =========================================================================*/
25:
26: in vec4 vertexMC;
27:
28:
29:
30: // frag position in VC
31: //VTK::PositionVC::Dec
32:
33: // optional normal declaration
34: //VTK::Normal::Dec
35:
36: // extra lighting parameters
37: //VTK::Light::Dec
38:
39: // Texture coordinates
40: //VTK::TCoord::Dec
41:
42: // material property values
43: //VTK::Color::Dec
44:
45: // clipping plane vars
46: //VTK::Clip::Dec
47:
48: // camera and actor matrix values
49: uniform mat4 MCDCMatrix;
50:
51: // Apple Bug
52: //VTK::PrimID::Dec
53:
54: // Value raster
55: //VTK::ValuePass::Dec
56:
57: // picking support
58: //VTK::Picking::Dec
59:
60: void main()
61: {
62: //VTK::Color::Impl
63:
64: //VTK::Normal::Impl
65:
66: //VTK::TCoord::Impl
67:
68: //VTK::Clip::Impl
69:
70: //VTK::PrimID::Impl
71:
72: gl_Position = MCDCMatrix * vertexMC;
73:
74:
75: //VTK::ValuePass::Impl
76:
77: //VTK::Light::Impl
78:
79: //VTK::Picking::Impl
80: }
ERROR:root:1: #version 150
2021-03-11 19:57:39.294 ( 1.008s) [ 84DF2C] vtkShaderProgram.cxx:438 ERR| vtkShaderProgram (0x7fee29ba49b0):
WARNING:root:ERROR: In /Users/tjcorona/Development/vtk/source/Rendering/OpenGL2/vtkShaderProgram.cxx, line 438
vtkShaderProgram (0x7fee29ba49b0):
Shadowing is currently not implemented in PyVista, and I don't know how reliable it is in vtk (or how to make it work reliably).
Quoting from this comment:
As for the shadows, it works 100% of the time 60% of the time. We should add as an option and then users can play around with it. I'm sure at some point real time ray tracing is going to be a hardware feature even down to integrated GPUs, so it's a matter of time until this becomes a well tested/supported feature.
For instance on my laptop with an integrated GPU shadow rendering is a mess.
With window size (1000, 1000):
With window size (2000, 1000):
In any case I can more or less reproduce the errors you're seeing. The first error comes partly from PyVista:
vtkLightActor.cxx:285 ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
This is because PyVista Light objects have a vtkLightActor attached to them, but these should be hidden (i.e. never rendered) unless the light is a spotlight (the only case where a vtkLightActor makes sense). As I noted on the aforementioned issue the error shouldn't arise and it makes little sense that shadowing affects this. If this ended up staying a problem we could try mitigating it in PyVista, but it would be a lot of complexity for questionable gain.
The second error from the shader is very much VTK and I don't know what exactly it's about. Considering how this is not a supported feature of PyVista and you're using vtk directly, it probably makes more sense to raise the issue with vtk. (Although I don't know if there might be something we could put in on the PyVista side to make it work more reliably.)
For what it's worth you might be able to swap your if shadows block with just this:
plotter.renderer.SetUseShadows(shadows)
At least I see the same behaviour if I use that. So in cases where your code works for you, it might be a simpler alternative.
I'm trying to design a logical calculator that can plus, multiply, division, ...
but I stuck at 1 part when I try to run it on the DE 10 board it doesn't act as the logic I though
My logic is that when count = 0, the operand A will be at HEX 4,5 and count = 1 DoOpt will be at 2, and count = 2 operand B will display at HEX 1,0, then count will be reset. However when I run it only shows operand B only. can you figure for me what is wrong with my code?
module group_project (
input logic clk, Set, AC,
input logic [3:0] Operand,
input logic [2:0] DoOp,
output logic [6:0] Seg5, Seg4, Seg3, Seg2, Seg1, Seg0
);
logic [1:0] count;
always_ff#(posedge clk) begin
if(!AC) begin
Seg5 = 7'b000_1000;
Seg4 = 7'b000_0110;
Seg3 = 7'b000_1000;
Seg2 = 7'b100_0000;
Seg1 = 7'b001_0001;
Seg0 = 7'b011_1111;
count <= 2'b00;
end
if(!Set && count == 2'b00) begin
count <= count + 1;
Seg3 = 7'b111_1111;
Seg2 = 7'b111_1111;
Seg1 = 7'b111_1111;
Seg0 = 7'b111_1111;
if(Operand < 4'b1010) begin
Seg5 = 7'b100_0000;
case (Operand)
4'b0000: Seg4 = 7'b100_0000;
4'b0001: Seg4 = 7'b111_1001;
4'b0010: Seg4 = 7'b010_0100;
4'b0011: Seg4 = 7'b011_0000;
4'b0100: Seg4 = 7'b001_1001;
4'b0101: Seg4 = 7'b001_0010;
4'b0110: Seg4 = 7'b000_0010;
4'b0111: Seg4 = 7'b111_1000;
4'b1000: Seg4 = 7'b000_0000;
4'b1001: Seg4 = 7'b001_0000;
endcase
end
else begin
Seg5 = 7'b111_1001;
case (Operand)
4'b1010: Seg4 = 7'b100_0000;
4'b1011: Seg4 = 7'b111_1001;
4'b1100: Seg4 = 7'b010_0100;
4'b1101: Seg4 = 7'b011_0000;
4'b1110: Seg4 = 7'b001_1001;
4'b1111: Seg4 = 7'b001_0010;
endcase
end
end
if(!Set && count == 2'b01) begin
count <= count + 1;
Seg5 = 7'b111_1111;
Seg4 = 7'b111_1111;
Seg3 = 7'b111_1111;
Seg1 = 7'b111_1111;
Seg0 = 7'b111_1111;
case(DoOp)
3'b001: Seg2 = 7'b111_1001;
3'b010: Seg2 = 7'b010_0100;
3'b011: Seg2 = 7'b011_0000;
3'b100: Seg2 = 7'b001_1001;
3'b101: Seg2 = 7'b001_0010;
endcase
end
/*else if(!Set && count == 2'b10) begin
Seg5 = 7'b111_1111;
Seg4 = 7'b111_1111;
Seg3 = 7'b111_1111;
Seg2 = 7'b111_1111;
count <= count;
if(Operand < 4'b1010) begin
Seg1 = 7'b100_0000;
case (Operand)
4'b0000: Seg0 = 7'b100_0000;
4'b0001: Seg0 = 7'b111_1001;
4'b0010: Seg0 = 7'b010_0100;
4'b0011: Seg0 = 7'b011_0000;
4'b0100: Seg0 = 7'b001_1001;
4'b0101: Seg0 = 7'b001_0010;
4'b0110: Seg0 = 7'b000_0010;
4'b0111: Seg0 = 7'b111_1000;
4'b1000: Seg0 = 7'b000_0000;
4'b1001: Seg0 = 7'b001_0000;
endcase
end
else begin
Seg1 = 7'b111_1001;
case (Operand)
4'b1010: Seg0 = 7'b100_0000;
4'b1011: Seg0 = 7'b111_1001;
4'b1100: Seg0 = 7'b010_0100;
4'b1101: Seg0 = 7'b011_0000;
4'b1110: Seg0 = 7'b001_1001;
4'b1111: Seg0 = 7'b001_0010;
endcase
end
end
end
endmodule
There's something to do with AC and Set.
If AC is used as initial reset only, and after that Set is constant low, then count will at last stay at 2. It only lasts 1 clk cycle for count = 0 and 1. So as long as clk is fast enough, you'll be unable to notice count = 0 and 1, and only see count = 2 state.
Perhaps you need to deassert AC periodically, and control Set to see the transition of each count state.
I am trying to do a very basic hardware module/test bench to get the hang of Verilog. I have tried to implement a full adder.
If I am not mistaken, you have three input, immediate addends a and b and a carry in from the 2^n-1 place.
The outputs are sum and carry out (which might serve as a carry in to another module in a basic adder or whatever the not carry-lookahead is called.)
If I am not mistaken the output logic is
sum = (a&b) | (a&cin) | (b&cin) //or all three, which is covered by any of these
cout = a ^ b ^ cin
Here is the full adder module
module FullAdder(
a,
b,
cin,
sum,
co
);
input a;
input b;
input cin;
output sum;
output co;
//wire a;
//wire b;
//wire ci;
wire sum;
wire co;
//At least two
assign co = (a & b) | (a & cin) | (b & cin);
//one or three
assign sum = a ^ b ^ cin; //(a & ~b & ~cin) | (~a & b & ~cin) | (~a & ~b & cin) | (a & b & cin);
endmodule
And here is the test bench
module HalfAdderTB();
reg a_in;
reg b_in;
reg cin_in;
wire s_out;
wire cout_out;
FullAdder DUT(
a_in,
b_in,
cin_in,
s_out,
cout_out
);
initial begin
a_in = 1'b0;
b_in = 1'b0;
cin_in = 1'b0;
#20
a_in = 1'b0;
b_in = 1'b0;
cin_in = 1'b0;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
a_in = 1'b0;
b_in = 1'b0;
cin_in = 1'b1;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
a_in = 1'b0;
b_in = 1'b1;
cin_in = 1'b0;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
a_in = 1'b0;
b_in = 1'b1;
cin_in = 1'b1;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
a_in = 1'b1;
b_in = 1'b0;
cin_in = 1'b0;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
a_in = 1'b1;
b_in = 1'b0;
cin_in = 1'b1;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
2,1 4%
a_in = 1'b1;
b_in = 1'b1;
cin_in = 1'b0;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
assign a_in = 1'b1;
assign b_in = 1'b1;
assign cin_in = 1'b1;
$display("a: %d, b: %d, cin: %d", a_in, b_in, cin_in);
$display("s: %b, cout: %b", s_out, cout_out);
#20
$finish;
end
endmodule
My output looks like this
a: 0, b: 0, cin: 0
s: 0, cout: 0
a: 0, b: 0, cin: 1
s: 0, cout: 0
a: 0, b: 1, cin: 0
s: 1, cout: 0
a: 0, b: 1, cin: 1
s: 1, cout: 0
a: 1, b: 0, cin: 0
s: 0, cout: 1
a: 1, b: 0, cin: 1
s: 1, cout: 0
a: 1, b: 1, cin: 0
s: 0, cout: 1
a: 1, b: 1, cin: 1
s: 0, cout: 1
I believe the logic statements in my code match those of the boolean equations I wrote up top. I am confident in my logic. I cannot seem to figure out what is wrong with the Verilog. Have I missed something with the timing and input of the test bench to the Full Adder?
Your code is fine, but you are getting this strange result due to $display statement. Your code will work fine, if you use $strobe instead of $display. You could also use $monitor to display the results. The reson is that display statement executes immediately so that your outputs will not yet be updated with the new values, whereas strobe will execute only at the end of a time instant, so that your outputs would have been updated by then. monitor is used to automatically display values, whenever the values see a change.
Since you are just starting on verilog, I would suggest you to go through this link to understand how various display statements work in verilog and also to go through this link to understand the order of execution of statements in a particular time instant, so that you can plan your code better
I am trying to write a single cycle mips verilog code which only contains selected instructions, to pass a simple test, it took two days for me to write below code but I checked instruction per instruction but getting to branch( first branch) instruction I really became desperate( the new pc value doesn't update correctly ).I would be so appreciated by any suggestion from you.
PS: the instructions before first branch are executed correctly.
here is the main code
`timescale 1ns/10ps
// R Format funcs
`define ADDU 6'b100001
`define ADD 6'b100000
`define SUB 6'b100011
`define AND 6'b100100
`define XOR 6'b100110
`define NOR 6'b100111
`define SLT 6'b101010
`define MULT 6'b011000
`define SLTU 6'b101011
`define MULTU 6'b011001
`define JALR 6'b001001
`define JR 6'b001000
`define MFHI 6'b010000
`define MFLO 6'b010010
// I & J Format op
`define ADDI 6'b001000
`define ADDIU 6'b001001
`define ORI 6'b001101
`define XORI 6'b001110
`define ANDI 6'b001100
`define SLTI 6'b001010
`define SLTIU 6'b001011
`define LUI 6'b001111
`define LW 6'b100011
`define SW 6'b101011
`define BEQ 6'b000100
`define BNE 6'b000101
`define J 6'b000010
`define JAL 6'b000011
// ALU OP
`define alu_add 4'b0000
`define alu_sub 4'b0001
`define alu_and 4'b0010
`define alu_or 4'b0011
`define alu_xor 4'b0100
`define alu_nor 4'b0101
`define alu_sltu 4'b0110
`define alu_slt 4'b0111
`define alu_addu 4'b1000
module single_cycle_mips(
input clk,
input reset
);
wire alu_z;
reg [3:0] alu_op;
reg [1:0] aluSrcB ,RegDst ;
reg [2:0] WD_e;
reg [4:0] Regno , flag=0;
reg aluSrcA, SE_ZEn, rf_wrt;
reg pc_wrt, ir_wrt, IorD, mar_wrt ,mult_s ,is_s,mem_wrt;
wire [31:0] rf_rd_1, rf_rd_2, alu_result ,s1,s2,mem_addr,mem_write_data,mem_read_data;
reg [31:0] pc, MemtoReg, ir,mar, aluB , aluA ,A ,B ,HI ,LO;
// CONTROLLER Starts Here
wire [5:0] ir_op = ir[31:26];
wire [5:0] funct = ir[5:0];
always #(ir) begin : MAIN
//nxt_state = 'bx;
alu_op = 'bx; aluSrcB = 'bx; aluSrcA = 'bx; SE_ZEn = 'bx;
rf_wrt = 1'b0; pc_wrt = 1'b0; ir_wrt = 1'b0; WD_e = 'bx;
mem_wrt = 1'b0; mult_s = 'bx; mar_wrt = 1'b0; Regno = 'bx;
is_s = 'bx; RegDst = 'bx ;
IorD = 0;
ir_wrt = 1'b1; // load IR by memory data
//aluSrcA = 1'b0; //
//aluSrcB = 2'b01; //
//alu_op = `alu_addu; //
//pc_wrt = 1'b1; // pc <- pc + 4
pc = pc + 4;
#8
case(ir_op)
6'b000000:begin // R-format
if(funct == `MFHI) begin
WD_e = 3'b010;
rf_wrt = 1'b1;
RegDst = 2'b01;
//nxt_state = FETCH1;
end
else if(funct == `MFHI) begin
WD_e = 3'b011;
rf_wrt = 1'b1;
RegDst = 2'b01;
//nxt_state = FETCH1;
end
else if((funct == `JALR) || (funct== `JR)) begin
A =rf_rd_1;
if(funct == `JALR) begin
rf_wrt = 1'b1;
WD_e = 3'b100;
RegDst = 2'b01;
end
pc = A;
end
else begin
A = rf_rd_1;
B = rf_rd_2;
#3
rf_wrt = 1'b1;
aluSrcA = 1'b1;
aluSrcB = 2'b00;
WD_e = 3'b001;
RegDst = 2'b01;
case(funct[5:0])
`ADD: begin alu_op = `alu_add; end
`ADDU: begin alu_op = `alu_addu; end
`SUB: begin alu_op = `alu_sub; end
`AND: begin alu_op = `alu_and; end
`XOR: begin alu_op = `alu_xor; end
`NOR: begin alu_op = `alu_nor; end
`SLTU: begin alu_op = `alu_sltu; end
`SLT: begin alu_op = `alu_slt; end
`MULT: begin
if(flag <=18 && flag!=0)
flag= flag+1;
else if(flag == 18) begin
HI <= #0.1 s2;
LO <= #0.1 s1;
flag = 0;
end
else if(flag==0) begin
mult_s = 1'b1;
is_s = 1'b1;
end
end
`MULTU: begin
if(flag <=18 && flag!=0)
flag= flag+1;
else if(flag == 18) begin
HI <= #0.1 s2;
LO <= #0.1 s1;
flag = 0;
//nxt_state = FETCH1;
end
else if(flag==0) begin
mult_s = 1'b1;
is_s = 1'b1;
end
end
endcase
end
end
`ADDI ,`ADDIU ,`ORI ,`XORI ,`ANDI ,`SLTI, `SLTIU: // I format
begin
A = rf_rd_1;
#3
rf_wrt = 1'b1;
aluSrcA = 1'b1;
aluSrcB = 2'b10;
WD_e = 3'b001;
RegDst = 2'b00;
case(ir_op)
`ADDI:begin
SE_ZEn =1'b1;
alu_op = `alu_add;
//nxt_state = FETCH1;
end
`ADDIU:begin
SE_ZEn =1'b0;
alu_op = `alu_addu;
//nxt_state = FETCH1;
end
`ANDI:begin
SE_ZEn =1'b1;
alu_op = `alu_and;
//nxt_state = FETCH1;
end
`ORI:begin
SE_ZEn =1'b0;
alu_op = `alu_or;
//nxt_state = FETCH1;
end
`XORI:begin
SE_ZEn =1'b0;
alu_op = `alu_xor;
//nxt_state = FETCH1;
end
`SLTI:begin
SE_ZEn =1'b1;
alu_op = `alu_slt;
//nxt_state = FETCH1;
end
`SLTIU:begin
SE_ZEn =1'b0;
alu_op = `alu_sltu;
//nxt_state = FETCH1;
end
endcase
end
`LUI: begin
WD_e = 3'b101;
rf_wrt = 1'b1;
RegDst = 2'b00;
end
`LW: begin
A = rf_rd_1;
mar_wrt = 1'b1;
aluSrcA = 2'b1;
aluSrcB = 2'b10;
SE_ZEn = 1;
alu_op = `alu_add;
#3
IorD = 1;
#8
rf_wrt = 1'b1;
WD_e = 3'b000;
RegDst = 2'b00;
//nxt_state = FETCH1;
end
`SW: begin
A = rf_rd_1;
B = rf_rd_2;
mar_wrt = 1'b1;
aluSrcA = 1'b1;
aluSrcB = 2'b10;
SE_ZEn =1'b1;
alu_op = `alu_add;
#3
IorD = 1;
ir_wrt = 0;
mem_wrt = 1;
#4 begin IorD = 0; mem_wrt = 0; ir_wrt = 1; end
//disable MAIN;
//nxt_state = FETCH1;
end
`BEQ ,`BNE: begin
A = rf_rd_1;
B = rf_rd_2;
aluSrcA = 1'b1;
aluSrcB = 2'b00;
alu_op = `alu_xor;
#3
if( ((alu_z == 0) && ((ir_op)== `BEQ) ) || ((alu_z == 1) && ((ir_op)== `BNE)))
disable MAIN;
//nxt_state = FETCH1;
else
begin
SE_ZEn =1'b1;
aluSrcA = 1'b0;
aluSrcB = 2'b11;
alu_op = `alu_add;
pc_wrt = 1'b1;
//nxt_state = FETCH1;
end
end
`J: begin
pc = {7'b00000,ir[25:0]};
//nxt_state = FETCH1;
end
`JAL: begin
rf_wrt = 1'b1;
RegDst = 2'b10;
WD_e = 3'b100;
pc = {7'b00000,ir[25:0]};
//nxt_state = FETCH1;
end
endcase
end
// CONTROLLER Ends Here
// DATA PATH Starts Here
always #(posedge clk)
if(reset) begin
pc <= #0.1 32'h00000000;
IorD = 0;
ir_wrt = 1'b1;
end
else if(pc_wrt)
pc <= #0.1 alu_result;
always #(posedge clk) if(ir_wrt) ir <= #0.1 mem_read_data;
always #* if(mar_wrt) mar <= #0.1 alu_result;
assign mem_write_data = B;
assign mem_addr = IorD ? mar : pc;
wire [31:0] SZout = SE_ZEn ? {{16{ir[15]}}, ir[15:0]} : {16'h0000, ir[15:0]};
always #(*) begin
case (aluSrcB)
2'b00: aluB <= B;
2'b01: aluB <= 32'h4;
2'b10: aluB <= SZout;
2'b11: aluB <= SZout << 2;
endcase
case (aluSrcA)
1'b0: aluA = pc;
1'b1: aluA = A;
endcase
case(RegDst)
2'b00: Regno <= ir[20:16];
2'b01: Regno <= ir[15:11];
2'b10: Regno <= 31;
endcase
case(WD_e)
3'b000: MemtoReg <= mem_read_data;
3'b001: MemtoReg <= alu_result;
3'b010: MemtoReg <= HI;
3'b011: MemtoReg <= LO;
3'b100: MemtoReg <= pc;
3'b101: MemtoReg <= {ir[15:0],16'h0000};
endcase
end
my_alu alu(
.aluA(aluA),
.aluB(aluB),
.aluOp(alu_op),
.aluResult(alu_result),
.aluZero(alu_z));
reg_file registers(
.clk(clk),
.write(rf_wrt),
.WR(Regno),
.WD(MemtoReg),
.RR1(ir[25:21]),
.RR2(ir[20:16]),
.RD1(rf_rd_1),
.RD2(rf_rd_2));
async_mem mem(
.clk(clk),
.write(mem_wrt),
.address(mem_addr),
.write_data(mem_write_data),
.read_data(mem_read_data));
multiplier u1(
.clk(clk),
.start(mult_s),
.is_signed(is_s),
.a(A),
.b(B),
.s1(s1),
.s2(s2));
// DATA PATH Ends Here
endmodule
module my_alu(
input [31:0] aluA,
input [31:0] aluB,
input [ 3:0] aluOp,
output reg[31:0] aluResult,
output aluZero
);
always #(*)
case(aluOp)
`alu_add : aluResult <= #2 aluA + aluB;//begin if( aluA[31] == aluB[31]) aluResult <= #2 aluA + aluB;
//else aluResult <= #2 aluA[31] ? ~aluA + aluB + 1'b1 : aluA + ~aluB +1'b1; end // add
`alu_addu : aluResult <= #2 aluA + aluB;
`alu_sub : aluResult <= #2 aluA + ~aluB + 1'b1; // sub
`alu_and : aluResult <= #2 aluA & aluB;
`alu_or : aluResult <= #2 aluA | aluB;
`alu_nor : aluResult <= #2 ~(aluA | aluB); // ?? ~ ( aluA | aluB )
`alu_xor : aluResult <= #2 aluA ^ aluB;
`alu_sltu : aluResult <= #2 aluA < aluB ? 1 : 32'h00000000;
`alu_slt: begin
if( aluA[31]!=aluB[31])begin
if( aluA[31]== 1)
aluResult <= #2 1;
else if( aluA[31]== 0)
aluResult <= #2 32'h00000000;
end
else if( aluA[31]==aluB[31] ) begin
if (aluA[31]== 0)
aluResult <= #2 aluA < aluB ? 1 : 32'h00000000;
else if( aluA[31]== 1)
aluResult <= #2 (~aluA+1'b1) < (~aluB+1'b1) ? 1 : 32'h00000000;
end
end
endcase
assign aluZero = ~ (|aluResult);
endmodule
module async_mem(
input clk,
input write,
input [31:0] address,
input [31:0] write_data,
output [31:0] read_data
);
reg [31:0] mem_data [0:1023];
assign #7 read_data = mem_data[ address[31:2] ];
always #(*)
if(write)
mem_data[ address[31:2] ] <= #2 write_data;
endmodule
module reg_file(
input clk,
input write,
input [4:0] WR,
input [31:0] WD,
input [4:0] RR1,
input [4:0] RR2,
output [31:0] RD1,
output [31:0] RD2
);
reg [31:0] rf_data [0:31];
assign #2 RD1 = rf_data[ RR1 ];
assign #2 RD2 = rf_data[ RR2 ];
always #(*) begin
if(write) begin
rf_data[ WR ] <= #0.1 WD;
`ifdef DEBUG
if(WR)
$display("$%0d = %x", WR, WD);
`endif
end
rf_data[0] <= #0.1 32'h00000000;
end
endmodule
and here is the hex of the test code:
[0x000000] 0x34080000 # ori $t0, $zero, 0 ($t0 = $zero | 0)
[0x000004] 0x24090060 # addiu $t1, $zero, 96 ($t1 = 96)
[0x000008] 0x3403DEAD # ori $v1, $zero, -8531 ($v1 = $zero | -8531)
[0x00000C] 0xAD030080 # sw $v1, 128($t0) (mem[$t0 + 128] = $v1)
[0x000010] 0x2129FFFF # addi $t1, $t1, -1 ($t1 = $t1 + -1)
[0x000014] 0x25080004 # addiu $t0, $t0, 4 ($t0 = $t0 + 4)
[0x000018] 0x00631020 # add $v0, $v1, $v1 ($v0 = $v1 + $v1)
[0x00001C] 0x00621026 # xor $v0, $v1, $v0 ($v0 = $v1 ^ $v0)
[0x000020] 0x3843BEEF # xori $v1, $v0, -16657 ($v1 = $v0 ^ -16657)
[0x000024] 0x1409FFF9 # bne $t1, $zero, -7 (if ($t1 != $zero) goto -7)
[0x000028] 0x20080004 # addi $t0, $zero, 4 ($t0 = 4)
[0x00002C] 0x20090060 # addi $t1, $zero, 96 ($t1 = 96)
[0x000030] 0x01294821 # addu $t1, $t1, $t1 ($t1 = $t1 + $t1)
[0x000034] 0x01294821 # addu $t1, $t1, $t1 ($t1 = $t1 + $t1)
[0x000038] 0x0109502A # slt $t2, $t0, $t1 (if ($t0 < $t1) $t2 = 1 else $t2 = 0)
[0x00003C] 0x1140000E # beq $t2, $zero, 14 (if ($t2 == $zero) goto 14)
[0x000040] 0x00085820 # add $t3, $zero, $t0 ($t3 = $t0)
[0x000044] 0x8D0C0080 # lw $t4, 128($t0) ($t4 = mem[$t0 + 128])
[0x000048] 0x000B502A # slt $t2, $zero, $t3 (if ($zero < $t3) $t2 = 1 else $t2 = 0)
[0x00004C] 0x11400007 # beq $t2, $zero, 7 (if ($t2 == $zero) goto 7)
[0x000050] 0x216DFFFC # addi $t5, $t3, -4 ($t5 = $t3 + -4)
[0x000054] 0x8DAE0080 # lw $t6, 128($t5) ($t6 = mem[$t5 + 128])
[0x000058] 0x01CC502B # sltu $t2, $t6, $t4 (if ($t6 < $t4) $t2 = 1 else $t2 = 0)
[0x00005C] 0x11400003 # beq $t2, $zero, 3 (if ($t2 == $zero) goto 3)
[0x000060] 0xAD6E0080 # sw $t6, 128($t3) (mem[$t3 + 128] = $t6)
[0x000064] 0x000D5820 # add $t3, $zero, $t5 ($t3 = $t5)
[0x000068] 0x1000FFF7 # beq $zero, $zero, -9 (if ($zero == $zero) goto -9)
[0x00006C] 0xAD6C0080 # sw $t4, 128($t3) (mem[$t3 + 128] = $t4)
[0x000070] 0x21080004 # addi $t0, $t0, 4 ($t0 = $t0 + 4)
[0x000074] 0x1000FFF0 # beq $zero, $zero, -16 (if ($zero == $zero) goto -16)
[0x000078] 0x1000FFFF # beq $zero, $zero, -1 (if ($zero == $zero) goto -1)
and here is the test bench:
`timescale 1ns/1ns
module multi_cycle_mips__tb;
reg clk = 1;
always #(clk)
clk <= #20 ~clk; //20
reg reset;
initial begin
reset = 1;
#(posedge clk);
#(posedge clk);
#(posedge clk);
#1;
reset = 0;
end
initial
$readmemh("isort32.hex", uut.mem.mem_data);
parameter end_pc = 32'h7C;
integer i;
always #(uut.pc)
if(uut.pc == end_pc) begin
for(i=0; i<96; i=i+1) begin
$write("%x ", uut.mem.mem_data[32+i]);
if(((i+1) % 16) == 0)
$write("\n");
end
$stop;
end
single_cycle_mips uut(
.clk(clk),
.reset(reset)
);
endmodule