Simulating MIPS processor on FPGA using Verilog - simulation

I am trying to simulate a simple MIPS processor on an FPGA using Verilog. Here is my code:
module MIPS_Processor(output reg[7:0] LEDs, input[7:0] Switches);
reg [31:0] memory[0:4095]; // 4K memory cells that are 32 bits wide
reg [31:0] code[0:1023]; // 1K memory cells that are 32 bits wide
reg [31:0] registers[0:31]; // 32 registers that are 32 bits wide
reg [31:0] PC; // The program counter
reg [31:0] instruction;
reg [5 :0] op;
reg [4 :0] rs;
reg [4 :0] rt;
reg [4 :0] rd;
reg [4 :0] shamt;
reg [5 :0] funct;
reg signed [15:0] immediate_offset;
reg [25:0] target;
reg [1:0] instruction_type; // 00 --> R | 01 --> I | 10 --> J | 11 --> EXTRA
reg [31:0] rs_value;
reg [31:0] rt_value;
reg [31:0] rd_value;
initial
begin
PC = 0;
/* Here we insert the code in the code array */
code[0] = 32'b00010010000000000000000000000000; // start : input s0 # read switches.
code[1] = 32'b00010010000000000000000000000001; // output s0 # write leds.
code[2] = 32'b00001000000000000000000000000000; // j start
code[3] = 32'b00000100000000000000000000000000; // END OF CODE
end
always
begin : loop_block
// 1. Fetch an instruction from memory
instruction = code[PC];
// 2. Increment the program counter register (by the instruction length)
PC = PC + 1;
// 3. Decode the instruction
/*
The instructions are:
6 5 5 5 5 6
_____________________________
or rd, rs, rt | 0 | rs | rt | rd | 0 | 0x25 |
6 5 5 16
_____________________________
ori rt, rs, immediate | 0xd | rs | rt | immediate |
6 5 5 5 5 6
_____________________________
and rd, rs, rt | 0 | rs | rt | rd | 0 | 0x24 |
6 5 5 16
_____________________________
andi rt, rs, immediate | 0xc | rs | rt | immediate |
6 5 5 16
_____________________________
beq rs, rt, offset | 4 | rs | rt | offset |
6 5 5 5 5 6
_____________________________
sub rd, rs, rt | 0 | rs | rt | rd | 0 | 0x22 |
6 5 5 5 5 6
_____________________________
add rd, rs, rt | 0 | rs | rt | rd | 0 | 0x20 |
6 5 5 16
_____________________________
addi rt, rs, immediate | 8 | rs | rt | immediate |
6 26
_____________________________
j target | 2 | target |
6 5 5 5 5 6
_____________________________
slt rd, rs, rt | 0 | rs | rt | rd | 0 | 0x2a |
6 5 5 16
_____________________________
lw rt, rs[offset] | 0x23 | rs | rt | offset |
6 5 5 16
_____________________________
sw rt, rs[offset] | 0x2b | rs | rt | offset |
::EXTRA INSTRUCTIONS::
6 5 21
_____________________________
input rs | 4 | rs | 0 |
6 5 21
_____________________________
output rs | 4 | rs | 1 |
*/
op[5:0] = instruction[31:26];
case(op)
0: /* R-type */
begin
rs = instruction[25:21];
rt = instruction[20:16];
rd = instruction[15:11];
shamt = instruction[10:6];
funct = instruction[5:0];
instruction_type = 2'b00;
end
1: /* END OF CODE */
begin
disable loop_block;
end
2: /* J-type */
begin
target = instruction[25:0];
instruction_type = 2'b10;
end
4: /* EXTRA */
begin
rs = instruction[25:21];
funct = instruction[20:0];
instruction_type = 2'b11;
end
default: /* I-type */
begin
rs = instruction[25:21];
rt = instruction[20:16];
immediate_offset = instruction[15:0];
instruction_type = 2'b01;
end
endcase
// 4. Fetch operands, if any, usually from registers
case(instruction_type)
2'b00: /* R-type */
begin
rs_value = registers[rs];
rt_value = registers[rt];
end
2'b01: /* I-type */
begin
rs_value = registers[rs];
end
2'b11: /* EXTRA */
begin
if(funct == 1) rs_value = registers[rs];
end
endcase
// 5. Perform the operation
case(instruction_type)
2'b00: /* R-type */
begin
case(funct)
2'h20: /* add rd, rs, rt */
begin
rd_value = rs_value + rt_value;
end
2'h22: /* sub rd, rs, rt */
begin
rd_value = rs_value - rt_value;
end
2'h24: /* and rd, rs, rt */
begin
rd_value = rs_value & rt_value;
end
2'h25: /* or rd, rs, rt */
begin
rd_value = rs_value | rt_value;
end
2'h2a: /* slt rd, rs, rt */
begin
rd_value = rs_value < rt_value? 1 : 0;
end
endcase
end
2'b01: /* I-type */
begin
case(op)
4: /* beq rs, rt, offset */
begin
if(rs_value < rt_value) PC = immediate_offset;
end
8: /* addi rt, rs, immediate */
begin
rt_value = rs_value + immediate_offset;
end
1'hc: /* andi rt, rs, immediate */
begin
rt_value = rs_value & immediate_offset;
end
1'hd: /* ori rt, rs, immediate */
begin
rt_value = rs_value | immediate_offset;
end
2'h23: /* lw rt, rs[offset] */
begin
rt_value = memory[rs + immediate_offset];
end
2'h2b: /* sw rt, rs[offset] */
begin
memory[rs + immediate_offset] = rt_value;
end
endcase
end
2'b10: /* J-type */
begin
case(op)
2: /* j target */
begin
PC = target;
end
endcase
end
2'b11: /* EXTRA */
begin
case(funct)
0: /* input rs */
begin
rs_value[7:0] = Switches;
end
1: /* output rs */
begin
LEDs = rs_value[7:0];
end
endcase
if(funct == 1) rs_value = registers[rs];
end
endcase
// 6. Store the results
case(instruction_type)
2'b00: /* R-type */
begin
registers[rd] = rd_value;
end
2'b01: /* I-type */
begin
case(op)
8: /* addi rt, rs, immediate */
begin
registers[rt] = rt_value;
end
1'hc: /* andi rt, rs, immediate */
begin
registers[rt] = rt_value;
end
1'hd: /* ori rt, rs, immediate */
begin
registers[rt] = rt_value;
end
2'h23: /* lw rt, rs[offset] */
begin
registers[rt] = rt_value;
end
endcase
end
2'b11: /* EXTRA */
begin
if(funct == 0) registers[rs] = rs_value;
end
endcase
#100; /* Delay */
end
endmodule
I have attched output reg[7:0] LEDs to 8 LEDs on the FPGA device, and input[7:0] Switches on 8 switches of the FPGA. The code is compiled without any error. But unfortunately, it doesn't work. The LEDs should show the states of the switches, but they are always turned off.
However, when I tried hardcode the LEDs states like LEDs[7:0] = 8'b11111111; inside the initial block, the LEDs stays turned on all the time. While when I placed LEDs[7:0] = 8'b11111111; inside the always block, the LEDs stays turned off all the time. It seems that the FPGA does not execute the code inside always block, what is wrong? Am I implementing the design in a wrong way?

You may be able to simulate this code with a Verilog simulator but you will not be able to synthesize this code and load it onto an FPGA. As the comments say, synthesizable Verilog code for FPGAs would have a clock. It should have constructs that look like this
always #* begin : combinational_logic
//...
end
always #(posedge clk) begin : sequential_logic
//...
end

The contents of your code array also look wrong. The array dissassembles as:
BEQ $16, $0, 0x4
BEQ $16, $0, 0x8
J 0x0
BLTZ $0, 0x4

Related

Simple sum in IJVM

Suppose we need to make a sum of two binary numbers in ijvm, for example:
100 + 11 = 111
Translating all in ijvm:
ILOAD arg1 //100
ILOAD arg2 // 11
IADD
ISTORE i
Without making any changes to the code, what the content of the variable i ?
as they represented in ijvm numbers? A simple add enough to make a sum? Why do I need a shift right or left?
The byte code would look like
0x15 0x02
0x15 0x03
0x60
0x36 0x01
where 0x02 is the offset from LV to arg1, 0x03 is the offset from LV to arg2, and 0x01 is the offset from LV to i.
The local variable i would have 111 in it, if local variable arg1 has 100 and local variable arg2 has 11.
The stack would look like this before the IADD
011 <- SP
100 <- operand stack starts here
011 <- LV + 3
100 <- LV + 2
xxx <- LV + 1
xxx <- LV
The stack would look like this after the ISTORE
011 <- LV + 3
100 <- LV + 2
111 <- LV + 1
xxx <- LV
The code you supplied will perform the addition. You do not need to shift.
Response to comment about overflow:
Create a grid for adding two 2-bit numbers. 00 01 10 11 across the top and along the side.
| 00 01 10 11|
00| |
01| |
10| |
11| |
In the grid, show the result of the arithmetic, excluding overflow. Circle the entries that cause overflow. Such a table might give you a clue for detecting overflow, without looking at the carry bit.

Migrating from mysql to oracle 12c

I'm migrating from MySQL to oracle i got the script generated on MySQL and when creating a view MySQL add this:
Begin DesignProperties =
Begin PaneConfigurations =
Begin PaneConfiguration = 0
NumPanes = 4
Configuration = "(H (1[40] 4[20] 2[20] 3) )"
End
Begin PaneConfiguration = 1
NumPanes = 3
Configuration = "(H (1 [50] 4 [25] 3))"
End
Begin PaneConfiguration = 2
NumPanes = 3
Configuration = "(H (1 [50] 2 [25] 3))"
End
Begin PaneConfiguration = 3
NumPanes = 3
Configuration = "(H (4 [30] 2 [40] 3))"
End
Begin PaneConfiguration = 4
NumPanes = 2
Configuration = "(H (1 [56] 3))"
End
Begin PaneConfiguration = 5
NumPanes = 2
Configuration = "(H (2 [66] 3))"
End
Begin PaneConfiguration = 6
NumPanes = 2
Configuration = "(H (4 [50] 3))"
End
Begin PaneConfiguration = 7
NumPanes = 1
Configuration = "(V (3))"
End
Begin PaneConfiguration = 8
NumPanes = 3
Configuration = "(H (1[56] 4[18] 2) )"
End
Begin PaneConfiguration = 9
NumPanes = 2
Configuration = "(H (1 [75] 4))"
End
Begin PaneConfiguration = 10
NumPanes = 2
Configuration = "(H (1[66] 2) )"
End
Begin PaneConfiguration = 11
NumPanes = 2
Configuration = "(H (4 [60] 2))"
End
Begin PaneConfiguration = 12
NumPanes = 1
Configuration = "(H (1) )"
End
Begin PaneConfiguration = 13
NumPanes = 1
Configuration = "(V (4))"
End
Begin PaneConfiguration = 14
NumPanes = 1
Configuration = "(V (2))"
End
ActivePaneConfig = 0
End
Begin DiagramPane =
Begin Origin =
Top = 0
Left = 0
End
Begin Tables =
Begin Table = "TB"
Begin Extent =
Top = 6
Left = 38
Bottom = 114
Right = 189
End
DisplayFlags = 280
TopColumn = 0
End
End
End
Begin SQLPane =
End
Begin DataPane =
Begin ParameterDefaults = ""
End
Begin ColumnWidths = 9
Width = 284
Width = 1500
Width = 1500
Width = 1500
Width = 1500
Width = 1500
Width = 1500
Width = 1500
Width = 1500
End
End
Begin CriteriaPane =
Begin ColumnWidths = 11
Column = 1440
Alias = 900
Table = 1170
Output = 720
Append = 1400
NewValue = 1170
SortType = 1350
SortOrder = 1410
GroupBy = 1350
Filter = 1350
Or = 1350
Or = 1350
Or = 1350
End
End
End
I have created the view with no problem on oracle
but since i´m new on oracle i don't know if this code is necessary or its just necessary on MySQL, if it is necessary how can i get it done on oracle?
This code looks like the extended properties for SQL Server Management Studio (SSMS) which are added to the view so your GUI Design layout will be loaded again if you open that view again in the GUI - but you said you are doing MySQL to Oracle not SQL Server to Oracle so not sure why you are using SSMS.
This isn't required for the view to work (in any database) only to set the layout of the designer view in SSMS so there is no need to add it into your Oracle view.
If you use Tasks -> Generate Scripts to export your VIEW code via SSMS you can disable scripting of extended properties in the Advanced Options by setting Script Extended Properties to false.

16-bit CRC-CCITT in Matlab

Given the calcCRC() C function shown below, what is the equivalent Matlab function?
16-bit CRC-CCITT in C:
/*
* FUNCTION: calcCRC calculates a 2-byte CRC on serial data using
* CRC-CCITT 16-bit standard maintained by the ITU
* ARGUMENTS: queue_ptr is pointer to queue holding are a to be CRCed
* queue_size is offset into buffer where to stop CRC calculation
* RETURNS: 2-byte CRC
*/
unsigned short calcCRC(QUEUE_TYPE *queue_ptr, unsigned int queue_size) {
unsigned int i=0, j=0;
unsigned short crc=0x1D0F; //non-augmented initial value equivalent to augmented initial value 0xFFFF
for (i=0; i<queue_size; i+=1) {
crc ^= peekByte(queue_ptr, i) << 8;
for(j=0;j<8;j+=1) {
if(crc & 0x8000) crc = (crc << 1) ^ 0x1021;
else crc = crc << 1;
}
}
return crc;
}
Below is the Matlab code I have come up with that seems to be equivalent but does not output the same results:
(Incorrect) 16-bit CRC-CCITT in Matlab:
function crc_val = crc_ccitt_matlab (message)
crc = uint16(hex2dec('1D0F'));
for i = 1:length(message)
crc = bitxor(crc,bitshift(message(i),8));
for j = 1:8
if (bitand(crc, hex2dec('8000')) > 0)
crc = bitxor(bitshift(crc, 1), hex2dec('1021'));
else
crc = bitshift(crc, 1);
end
end
end
crc_val = crc;
end
Here is a sample byte array, represented as an integer array:
78 48 32 0 251 0 215 166 201 0 1 255 252 0 1 2 166 255 118 255 19 0 0 0 0 0 0 0 0 0 0 0 0 3 0
The expected output is two bytes base10(44 219) which is base2(00101100 11011011) or base10(11483).
My Matlab function gives base10(85) which is base2(00000000 01010101).
Any ideas on what is causing the output to not be the expected?
You should try bitsll() instead of bitshift(). The former is guaranteed to do what you want, whereas the behavior of the latter depends on properties of crc.
You will also need to and with 0xffff at the end.

array definition and initiation in promela

1 #define NUM_PHIL 4
2
3 byte chopstick[4];
4 chopstick[0] = 1;
5 chopstick[1] = 1;
6 chopstick[2] = 1;
7 chopstick[3] = 1;
8
9 proctype phil(int id) {
10 do
11 ::printf("Philosopher %d is thinking\n",id);
12 /* ... */
13 printf("Philosopher %d is eating with forks %d and %d\n",id,id,(id+1)%4);
14 /* ... */
15 od
16 }
16a
17 init {
18 int i = 0;
19 do
20 :: i >= NUM_PHIL -> break
21 :: else -> run phil(i);
22 i++
23 od
24 }
the codes above send an error "syntax error saw 'an identifier' near 'chopstick'"
how can i define and initialize array as a global variable outside the prototype P()
thanks for helping
You would initialize the chopstick array in an init body.
#define NUM_PHIL 4
byte chopstick[4]; /* NUM_PHIL */
proctype phil (int n) { /* ... */ }
init {
chopstick[0] = 1;
/* ... */
run phil (0);
/* ... */
}

How can I remove sp_addextendedproperty from a SQL Server view script?

I am doing a comparison of my Dev and QA databases. My Dev database has a bunch of sp_addextendedproperty information, which prevents the scripts from being an exact match. I tried dropping and recreating the offending view, but the noise is still there. How can I remove sp_addextendedproperty from a SQL Server 2008 R2 view script?
EXECUTE sp_addextendedproperty #name = N'MS_DiagramPane1', #value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties =
Begin PaneConfigurations =
Begin PaneConfiguration = 0
NumPanes = 4
Configuration = "(H (1[40] 4[20] 2[20] 3) )"
End
Begin PaneConfiguration = 1
NumPanes = 3
Configuration = "(H (1 [50] 4 [25] 3))"
End
Begin PaneConfiguration = 2
NumPanes = 3
Configuration = "(H (1 [50] 2 [25] 3))"
End
Begin PaneConfiguration = 3
NumPanes = 3
Configuration = "(H (4 [30] 2 [40] 3))"
End
Begin PaneConfiguration = 4
NumPanes = 2
Configuration = "(H (1 [56] 3))"
End
Begin PaneConfiguration = 5
NumPanes = 2
Configuration = "(H (2 [66] 3))"
End
Begin PaneConfiguration = 6
NumPanes = 2
Configuration = "(H (4 [50] 3))"
End
Begin PaneConfiguration = 7
NumPanes = 1
Configuration = "(V (3))"
End
Begin PaneConfiguration = 8
NumPanes = 3
Configuration = "(H (1[56] 4[18] 2) )"
End
Begin PaneConfiguration = 9
NumPanes = 2
Configuration = "(H (1 [75] 4))"
End
Begin PaneConfiguration = 10
NumPanes = 2
Configuration = "(H (1[66] 2) )"
End
Begin PaneConfiguration = 11
NumPanes = 2
Configuration = "(H (4 [60] 2))"
End
Begin PaneConfiguration = 12
NumPanes = 1
Configuration = "(H (1) )"
End
Begin PaneConfiguration = 13
NumPanes = 1
Configuration = "(V (4))"
End
Begin PaneConfiguration = 14
NumPanes = 1
Configuration = "(V (2))"
End
ActivePaneConfig = 0
End
Begin DiagramPane =
Begin Origin =
Top = 0
Left = 0
End
Begin Tables =
Begin Table = "apc"
Begin Extent =
Top = 6
Left = 38
Bottom = 125
Right = 236
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "p"
Begin Extent =
Top = 6
Left = 274
Bottom = 125
Right = 459
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "a"
Begin Extent =
Top = 6
Left = 497
Bottom = 125
Right = 669
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "ch"
Begin Extent =
Top = 6
Left = 707
Bottom = 95
Right = 867
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "c"
Begin Extent =
Top = 6
Left = 905
Bottom = 125
Right = 1065
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "r"
Begin Extent =
Top = 96
Left = 707
Bottom = 185
Right = 867
End
DisplayFlags = 280
TopColumn = 0
End
End
End
Begin SQLPane =
End
Begin DataPane =
Begin ParameterDefaults = ""
End
Begin ColumnWidths = 9
Width = 284
Width = 1500
Width = 1500
Width = 1500
Width = 1500', #level0type = N'SCHEMA', #level0name = N'dbo', #level1type = N'VIEW', #level1name = N'vwAccountProfitCenters';
GO
EXECUTE sp_addextendedproperty #name = N'MS_DiagramPane2', #value = N'
Width = 1500
Width = 1500
Width = 1500
Width = 1500
End
End
Begin CriteriaPane =
Begin ColumnWidths = 11
Column = 1440
Alias = 900
Table = 1170
Output = 720
Append = 1400
NewValue = 1170
SortType = 1350
SortOrder = 1410
GroupBy = 1350
Filter = 1350
Or = 1350
Or = 1350
Or = 1350
End
End
End
', #level0type = N'SCHEMA', #level0name = N'dbo', #level1type = N'VIEW', #level1name = N'vwAccountProfitCenters';
GO
EXECUTE sp_addextendedproperty #name = N'MS_DiagramPaneCount', #value = 2, #level0type = N'SCHEMA', #level0name = N'dbo', #level1type = N'VIEW', #level1name = N'vwAccountProfitCenters';
GO
In SQL compare (v10 is the one I have), once you've done your table mappings, there is another tab called options. On here under the ignore section is a tick box for extended properties, tick that and then it won't matter if there are different properties on each table.
As #Tarzan mentions in a comment below:
Yes, from SSMS, go to "Tools > Options > SQL Server Object Explorer > scripting > Script extended properties = False".