2023-03-09 06:13:15 +00:00
|
|
|
/* system.v - A basic test system with memory and IO for the 9086 CPU
|
|
|
|
|
|
|
|
This file is part of the 9086 project.
|
|
|
|
|
|
|
|
Copyright (c) 2023 Efthymios Kritikos
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2023-03-05 00:10:55 +00:00
|
|
|
`timescale 1ns/1ps
|
2023-05-07 12:34:15 +00:00
|
|
|
`include "error_header.v"
|
2023-05-14 15:06:33 +00:00
|
|
|
`include "config.v"
|
2023-03-05 00:10:55 +00:00
|
|
|
|
2023-03-04 08:37:43 +00:00
|
|
|
|
2023-11-25 04:11:51 +00:00
|
|
|
module system ( input clock,input reset, output [19:0]address_bus, output [15:0]data_bus_write ,output BHE, output rd, output wr, output IOMEM, output HALT, output [`ERROR_BITS-1:0] ERROR);
|
2023-05-14 15:06:33 +00:00
|
|
|
|
|
|
|
`ifdef CALCULATE_IPC
|
|
|
|
wire new_instruction;
|
|
|
|
`endif
|
|
|
|
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-05-14 15:06:33 +00:00
|
|
|
wire unsigned [`L1_CACHE_SIZE-1:0] L1_SIZE_STAT;
|
2023-05-21 01:59:53 +00:00
|
|
|
wire VALID_INSTRUCTION_STAT,jump_req;
|
2023-05-14 15:06:33 +00:00
|
|
|
`endif
|
|
|
|
|
2023-11-25 04:11:51 +00:00
|
|
|
wire [15:0]data_bus_read_CPU,data_bus_write_CPU;
|
2023-11-02 21:48:12 +00:00
|
|
|
|
2023-11-25 04:11:51 +00:00
|
|
|
assign data_bus_read_CPU=(IOMEM==0)?data_bus_read_RAM:data_bus_IO;
|
|
|
|
assign data_bus_write=data_bus_write_CPU;
|
2023-11-02 21:48:12 +00:00
|
|
|
|
2023-05-14 15:06:33 +00:00
|
|
|
processor p(
|
|
|
|
/* MISC */ clock,reset,HALT,ERROR
|
2023-11-25 04:11:51 +00:00
|
|
|
/* MEMORY / IO */ ,address_bus,data_bus_read_CPU,data_bus_write_CPU,rd,wr,BHE,IOMEM
|
2023-05-14 15:06:33 +00:00
|
|
|
`ifdef CALCULATE_IPC
|
|
|
|
/* STATISTICS */ ,new_instruction
|
|
|
|
`endif
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-05-21 01:59:53 +00:00
|
|
|
/* */ ,L1_SIZE_STAT, VALID_INSTRUCTION_STAT, jump_req
|
2023-05-14 15:06:33 +00:00
|
|
|
`endif
|
|
|
|
);
|
2023-03-08 07:26:28 +00:00
|
|
|
|
2023-11-25 04:11:51 +00:00
|
|
|
wire [15:0] data_bus_read_RAM;
|
|
|
|
|
|
|
|
doublemem sysmem(address_bus,data_bus_read_RAM,data_bus_write_CPU,rd,wr,BHE,IOMEM,clock);
|
2023-03-04 08:37:43 +00:00
|
|
|
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-01 06:03:53 +00:00
|
|
|
string stats_name,version,commit;
|
2023-05-14 15:06:33 +00:00
|
|
|
integer json_file_descriptor;
|
|
|
|
`endif
|
2023-11-02 00:29:14 +00:00
|
|
|
|
|
|
|
`ifndef YOSYS
|
2023-03-05 00:10:55 +00:00
|
|
|
string waveform_name;
|
2023-11-02 00:29:14 +00:00
|
|
|
`endif
|
|
|
|
|
2023-03-05 00:10:55 +00:00
|
|
|
initial begin
|
2023-11-02 23:46:12 +00:00
|
|
|
`ifndef SYNTHESIS
|
2023-11-02 00:29:14 +00:00
|
|
|
if($value$plusargs("WAVEFORM=%s",waveform_name))begin
|
|
|
|
$dumpfile(waveform_name);
|
|
|
|
$dumpvars(0,p,cycles);
|
|
|
|
end
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-04 08:31:05 +00:00
|
|
|
if(!$value$plusargs("VERSION=%s",version)) version="unkown";
|
|
|
|
if(!$value$plusargs("COMMIT=%s",commit)) commit="unkown";
|
|
|
|
if($value$plusargs("STATS=%s",stats_name))begin
|
|
|
|
json_file_descriptor=$fopen(stats_name,"w");
|
|
|
|
$fdisplay(json_file_descriptor,"{\n\"L1_size\":%0d,\n\"9086 verison\":\"%s\",\n\"latest commit\":\"%s\",\n\"Cycles\":[",$rtoi($pow(2,`L1_CACHE_SIZE)),version,commit);
|
|
|
|
first_json_cycle = 1;
|
|
|
|
end else
|
|
|
|
json_file_descriptor=0;
|
2023-11-02 00:29:14 +00:00
|
|
|
`endif
|
2023-11-02 23:46:12 +00:00
|
|
|
sane=0;
|
|
|
|
finish=0;
|
2023-05-14 15:06:33 +00:00
|
|
|
`endif
|
2023-03-05 00:10:55 +00:00
|
|
|
end
|
|
|
|
|
2023-05-07 12:34:15 +00:00
|
|
|
//integer killswitch=0;
|
|
|
|
//always @(posedge clock) begin
|
|
|
|
// killswitch <= killswitch +1;
|
|
|
|
// if( killswitch == 20000 )begin
|
|
|
|
// if($value$plusargs("MEMDUMP=%s",memdump_name))begin
|
|
|
|
// $writememh(memdump_name, system.sysmem.memory,0,32767);
|
|
|
|
// end
|
|
|
|
// $finish;
|
|
|
|
// end
|
|
|
|
//end
|
|
|
|
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-05-14 15:06:33 +00:00
|
|
|
reg first_json_cycle;
|
|
|
|
always @(negedge clock)begin
|
2023-10-31 19:01:34 +00:00
|
|
|
if(finish < 2 && json_file_descriptor!=0 && sane)begin
|
2023-05-21 01:59:53 +00:00
|
|
|
$fdisplay(json_file_descriptor,"%s{\"C\":%0d,\"L1\":%0d,\"VDI\":%0d,\"JMP\":%0d}",first_json_cycle?"":",",cycles,L1_SIZE_STAT,VALID_INSTRUCTION_STAT,jump_req);
|
2023-05-14 15:06:33 +00:00
|
|
|
first_json_cycle <= 0;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
`endif
|
|
|
|
|
2023-11-25 04:11:51 +00:00
|
|
|
reg [15:0]data_bus_IO;
|
|
|
|
|
2023-11-02 22:19:15 +00:00
|
|
|
`ifndef SYNTHESIS
|
2023-03-09 06:02:41 +00:00
|
|
|
always @(negedge wr) begin
|
2023-11-06 08:12:58 +00:00
|
|
|
if(IOMEM==1'b1 && address_bus[7:0]==8'hA5 )begin
|
2023-11-02 21:48:12 +00:00
|
|
|
$write("%s" ,data_bus_write[15:8]);
|
2023-11-06 08:12:58 +00:00
|
|
|
end else if(IOMEM==1'b1 && address_bus[7:0]==8'hB0 )begin
|
|
|
|
if(data_bus_write[0:0]==1)
|
|
|
|
$display("\x1b[7mLed turned on\x1b[m\n");
|
|
|
|
else
|
|
|
|
$display("\x1b[7mLed turned off\x1b[m\n");
|
|
|
|
end
|
2023-03-09 06:02:41 +00:00
|
|
|
end
|
2023-11-25 04:11:51 +00:00
|
|
|
always @(negedge rd) begin
|
|
|
|
if(IOMEM==1'b1 && address_bus[7:1]==7'h10 )begin // 0xABCD on address 0x20
|
|
|
|
data_bus_IO<=16'hABCD;
|
|
|
|
end else begin
|
|
|
|
data_bus_IO<=16'h0000;
|
|
|
|
end
|
|
|
|
end
|
2023-11-02 22:19:15 +00:00
|
|
|
`endif
|
2023-03-09 06:02:41 +00:00
|
|
|
|
2023-03-05 00:10:55 +00:00
|
|
|
|
2023-05-14 15:06:33 +00:00
|
|
|
`ifdef CALCULATE_IPC
|
2023-11-12 00:07:33 +00:00
|
|
|
/* verilator lint_off MULTIDRIVEN */
|
2023-05-23 08:27:46 +00:00
|
|
|
reg [128:0] instruction_count;
|
2023-11-12 00:07:33 +00:00
|
|
|
/* verilator lint_on MULTIDRIVEN */
|
2023-05-14 15:06:33 +00:00
|
|
|
always @(new_instruction) begin
|
|
|
|
instruction_count<=instruction_count+1;
|
|
|
|
end
|
|
|
|
`endif
|
|
|
|
|
2023-11-02 00:29:14 +00:00
|
|
|
|
2023-11-04 08:31:05 +00:00
|
|
|
`ifndef SYNTHESIS
|
|
|
|
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-04 08:31:05 +00:00
|
|
|
reg [128:0] instruction_count_temp;
|
|
|
|
`endif
|
|
|
|
|
|
|
|
string memdump_name;
|
|
|
|
|
|
|
|
always @(posedge HALT) begin
|
2023-11-02 00:29:14 +00:00
|
|
|
if($value$plusargs("MEMDUMP=%s",memdump_name))begin
|
|
|
|
$writememh(memdump_name, sysmem.memory,0,32767);
|
|
|
|
end
|
2023-11-04 08:31:05 +00:00
|
|
|
finish<=2'd1;
|
|
|
|
end
|
2023-03-05 00:10:55 +00:00
|
|
|
|
2023-11-12 00:07:33 +00:00
|
|
|
/* verilator lint_off MULTIDRIVEN */
|
2023-11-04 08:31:05 +00:00
|
|
|
reg [1:0] finish;
|
2023-11-12 00:07:33 +00:00
|
|
|
/* verilator lint_on MULTIDRIVEN */
|
2023-03-04 08:37:43 +00:00
|
|
|
|
2023-11-02 22:19:15 +00:00
|
|
|
reg sane;
|
|
|
|
reg [128:0] cycles;
|
|
|
|
|
|
|
|
always @(posedge reset)begin
|
|
|
|
sane<=1;
|
|
|
|
end
|
|
|
|
|
|
|
|
always @(posedge clock) begin
|
|
|
|
/* Allow some clock cycles for the waveform*/
|
|
|
|
case(finish)
|
|
|
|
2'd0: begin end
|
|
|
|
2'd1: begin
|
|
|
|
finish <= 2;
|
|
|
|
/* instruction_count gets updated at the sme time as HALT is pulled so wait a clock cycle to get an accurate reading*/
|
|
|
|
$display("\x1b[7mProcessor halted.\nCycles run for : %0d\x1b[m",cycles);
|
|
|
|
`ifdef CALCULATE_IPC
|
|
|
|
/* verilator lint_off REALCVT */
|
|
|
|
$display("\x1b[7mInstr. per cycle : %f\x1b[m", $itor(instruction_count) / $itor(cycles) );
|
|
|
|
/* verilator lint_on REALCVT */
|
|
|
|
`endif
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-02 22:19:15 +00:00
|
|
|
instruction_count_temp <= instruction_count;
|
|
|
|
`endif
|
2023-05-07 12:34:15 +00:00
|
|
|
end
|
2023-11-02 22:19:15 +00:00
|
|
|
2'd2: begin
|
|
|
|
finish <= 3;
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-02 22:19:15 +00:00
|
|
|
if(json_file_descriptor!=0)
|
|
|
|
$fdisplay(json_file_descriptor,"],\n\"Total Cycles\":%0d,\n\"Instructions run\":%0d\n}",cycles-1,instruction_count_temp);
|
|
|
|
`endif
|
2023-11-04 11:04:22 +00:00
|
|
|
$finish;
|
2023-05-07 12:34:15 +00:00
|
|
|
end
|
2023-11-02 22:19:15 +00:00
|
|
|
2'd3: begin
|
2023-05-07 12:34:15 +00:00
|
|
|
end
|
|
|
|
endcase
|
2023-03-05 00:10:55 +00:00
|
|
|
end
|
|
|
|
|
2023-11-02 22:19:15 +00:00
|
|
|
always @( ERROR ) begin
|
|
|
|
if ( ERROR != `ERR_NO_ERROR && sane == 1 ) begin
|
|
|
|
$display("PROCESSOR RUN INTO AN ERROR.");
|
|
|
|
case (ERROR)
|
|
|
|
default:begin
|
|
|
|
end
|
|
|
|
`ERR_UNIMPL_INSTRUCTION:begin
|
|
|
|
$display("Unimplemented instruction");
|
|
|
|
end
|
|
|
|
`ERR_UNIMPL_ADDRESSING_MODE: begin
|
|
|
|
$display("Unimplemented addressing mode");
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
$display("Cycles run for: %0d",cycles-1);
|
|
|
|
if($value$plusargs("MEMDUMP=%s",memdump_name))begin
|
|
|
|
$writememh(memdump_name, system.sysmem.memory,0,32767);
|
|
|
|
end
|
|
|
|
finish<=2'd1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-02 23:46:12 +00:00
|
|
|
always @(negedge clock)begin
|
|
|
|
if(reset==1)
|
|
|
|
cycles<=cycles+1;
|
|
|
|
else begin
|
|
|
|
cycles<=0;
|
2023-11-15 14:37:46 +00:00
|
|
|
`ifdef OUTPUT_JSON_STATISTICS
|
2023-11-02 23:46:12 +00:00
|
|
|
instruction_count <= 0;
|
|
|
|
`endif
|
|
|
|
end
|
2023-05-14 15:06:33 +00:00
|
|
|
end
|
2023-11-12 00:07:33 +00:00
|
|
|
|
2023-11-02 23:46:12 +00:00
|
|
|
`endif
|
|
|
|
|
2023-03-05 00:10:55 +00:00
|
|
|
|
|
|
|
|
2023-03-04 08:37:43 +00:00
|
|
|
endmodule
|