I'm trying to do some image processing with FPGA and my supporter want us to show some simulation result with Modelsim.
So, basically we try to read image file in testbench and write it to another file but it stop read file at half of the file. Here is my source code
module fileio1;
integer in,out,r;
reg [31:0]temp;
reg clk;
initial
begin
r=0;
temp =0;
clk = 0;
in = $fopen("test120.bmp","r");
out = $fopen("result.bmp","w");
end
always #1 clk = ~clk;
always #(negedge clk)
begin
r = $fscanf(in,"%c",temp);
end
always #(posedge clk)
begin
if(~r) $fwrite(out,"%c",temp);
end
endmodule
This is that source code and our input file is 120x180 size bitmapfile (64kb)
but output file is 38kb. Almost half of file. I try it with 480x720 size bitmapfile(1013kb), it's ouput file is almost half of the origin file too.
With very small size file input, we can get the right outputfile.
Why is this happen? Is there some better function to input/output file?
You should not use $fscanf in that situation. $fscanf skips over white-space, including blank lines (just like fscanf() function in C).
You should rather use $fread function:
always #(negedge clk)
begin
r = $fread(temp,in);
end
Related
I know yosys has limited tri-state support, but I'm looking for a possible workaround.
The following circuit:
module TBUF2
(
inout SALIDA1,
inout SALIDA2,
input OE,
output C);
assign SALIDA1=OE ? 1'b0 : 1'bZ;
assign SALIDA2=OE ? 1'b0 : 1'bZ;
wire e;
assign e=SALIDA1 & SALIDA2;
assign C=e;
endmodule
Is interpreted as:
TBUF2 parsed tree
Note that when OE is 0 C=SALIDA1 and SALIDA2.
During the opt pass, the opt_merge pass removes $2 mux and generates:
TBUF2 optimized
This breaks the circuit (when OE is 0 then C=SALIDA1). I realize this is because yosys/ABC doesn't really understand the consequences of the "1'z" input.
Is it possible to keep muxes that meet the following criteria?:
1) At least one input is 1'Z
2) Its output drives an inout pin
Here is the script to reproduce it:
read_verilog tbuf2.v
proc
show -format dot -prefix tbuf2_01
opt
show -format dot -prefix tbuf2_02
Convert the tristate buffer $mux cells to $tribuf cells by running the tribuf command after proc and before running any opt commands.
My problem seems really simple but somehow I can't get it to work.
I am trying to create a module in verilog which outputs a 3bit signal. I've tried so many different methods so far but none of them seem to work.
I am using Veriwell to compile my code and since I am doing this for university I am oblieged to use it so please don't recommend me different compilers.
Here are some methods I've tried but none of them worked
(I always get a 'Port sizes don't match in port #2' warning)
module testmod (in, out);
input [2:0] in;
output [2:0] out;
wire [2:0] out;
assign out = in; //trying just to connect in to out
endmodule
module testmod (in, out);
input [2:0] in;
output [2:0] out;
wire [2:0] out;
assign out = 3'b1; //trying to assign a random value to out
endmodule
module testmod (in, out);
input [2:0] in;
output [2:0] out;
reg [2:0] test;
always #(in)
test = in; //saving in in a register (which i believe is unnecessary but I've tried it nonetheless)
wire [2:0] out;
assign out = test; //trying just to connect in to out
endmodule
I instanciate the module like this:
testmod mod (.in(3'b1), .out(out));
Declaring out as a 3-bit signal in my testbench did the job. Since I've only worked with 1-bit output signals which apparently don't need to be declared at all in the testbench this didn't even come to my mind.
I'm trying to make a script for Trace32 PRACTICE language that reads some data from a .txt file and then passes the values to a variable that I need to debug.
My problem is that I cannot make it return to the top of the file and start over, once the .txt reaches the end. Any ideas ?
Here is what I have at the moment and it enters the IF from the start :
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
PRIVATE &value // declare macro
WHILE TRUE()
(
if FILE.EOFLASTREAD()
(
CLOSE #1
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
)
else
(
READ #1 %LINE &line
&value=STRing.TRIM(STRing.SPLIT("&line",",",0))
PRINT "&value"
V canConfigInitFlag = &value
)
)
CLOSE #1
ENDDO
Thanks !
Closing and re-opening the file to return to the top of the file is the correct approach. (There is no seek-command.)
However there is a bug in your script:
The function FILE.EOFLASTREAD() returns TRUE() if the last READ command did only return empty data because you've reached the end of the file. In your code you do no longer execute the READ command once you've reached the end of the file. Thus FILE.EOFLASTREAD() will continue to return TRUE().
The following will work:
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
PRIVATE &value &line // declare macros
WHILE TRUE()
(
READ #1 %LINE &line
IF FILE.EOFLASTREAD() // Last READ was after the end of the file
(
CLOSE #1
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
READ #1 %LINE &line // Read 1st line
)
&value=STRing.TRIM(STRing.SPLIT("&line",",",0))
PRINT "&value"
Var.set canConfigInitFlag=&value
)
CLOSE #1
ENDDO
I don't understand the envi_setup_head. Could anyone help me write it in IDL code format?
I have maps that were produced in IDL and I need to process them in ENVI. I don't know how to save the images in a folder and be able open them in ENVI. Does anyone know how to do it?
To create an ENVI header for an image file, you can try something like the IDL procedure below. It creates a small image file and uses envi_setup_head to create an ENVI header file. Essentially all you have to do is provide it with the number of samples, lines, data-type, etc., and you are good to go.
pro enviHeaderTest
compile_opt idl2
; Create the data and write to a file.
ns = 100
nl = 100
data = dist(ns, nl)
fname = 'mydatafile.dat'
openw, lun, fname, /GET_LUN
writeu, lun, data
close, lun
; Open a headless ENVI.
nv = envi(/HEADLESS)
; Create some map info for the raster.
mc = [0,0,0,0] ;Tie point: [x pixel, ypixel, x map, y map]
ps = [1D/3600, 1D/3600] ; Pixel size
mapInfo = envi_map_info_create(/GEOGRAPHIC, MC=mc, PS=ps)
; Create the header.
envi_setup_head, FNAME=fname, $ ; file name
NS=ns, $ ; number of samples
NL=nl, $ ; number of lines
NB=1, $ ; number of bands
DATA_TYPE=4, $ ; IDL data type (float in this case)
INTERLEAVE=0, $ ; BSQ
MAP_INFO=mapInfo, $
/WRITE
; Close ENVI.
nv.close
end
Then, you can read the image into ENVI, either from the File->Open menu, or via the IDL command line like so:
IDL> nv = envi()
ENVI> view = nv.getview()
ENVI> raster = nv.openraster('mydatafile.dat')
ENVI> layer = view.createlayer(raster)
I am having some trouble reading input in from a file. So what I have done is made a proof of concept program, which is a piece of my main program that does much more but I am only having trouble reading the input.
Here is my proof of concept program:
WITH Ada.Text_IO; USE Ada.Text_IO;
with ada.Integer_Text_IO; use ada.Integer_Text_IO;
PROCEDURE Open_File IS
subtype idnum is string(1 ..7);
-- Make short names so that we can show where things come from
My_File : File_Type; -- Name for file in this program
Os_Name : String := "My_Data.txt"; -- OS name for the file
N : idnum; -- Temporary for reading and printing file contents
EOL : boolean;
C : character;
BEGIN
-- Open will raise an ADA.IO_EXCEPTIONS.NAME_ERROR expection
-- if the file does not exist.
Open (File => My_File, Mode => In_File, Name => Os_Name);
LOOP
EXIT WHEN End_Of_File (My_File);
Look_Ahead(My_File, C, EOL);
IF EOL THEN
Skip_Line;
ELSE
IF C = ' ' THEN
Get(My_File, C);
ELSE
Get (My_File, N);
Put_Line(N);
END IF;
END IF;
END LOOP;
Close (My_File);
END open_file;
My data file looks like this: (including the spaces with no new lines after the last id)
1234567
456784a
6758abc
When I compile and run my program only the first id number gets printed to the screen. I have no clue where to check my code because it should continue to get id numbers until the end of the file.
Any help would be greatly appreciated. Thanks!
When you Get the second (and third, for that matter) line, Data_Error exception will be raised, because 456784a is not a number, 'a' is not a numeric character. If you want it to be a hexadecimal number, the input should be 16#456784a# (by default).