VHDL Report Group16
VHDL Report Group16
PROJECT REPORT
DIGITAL DESIGN USING VHDL
Hanoi, 12/2024
TABLE OF CONTENTS
ABBREVIATIONS i
LIST OF FIGURES ii
Acknowledgement iii
Task Assignment iv
Abstract 1
1 Introduction 2
1.1 Project Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
5 Conclusion 26
ABBREVIATIONS
B Blue
BMP Bitmap
BGR Blue Green Red
CPU Central Processing Unit
FPGA Field Programmable Gate Array
FSM Finite State Machine
G Green
GPU Graphic Processing Unit
HDL Hardware Design Language
Hex Hexadecimal
I/O In/Out
JPG Joint Photographic Experts Group
ms Microsecond
ns Nanosecond
PNG Portable Network Graphics
R Red
RAM Randon Access Memory
RGB Red Green Blue
i
LIST OF FIGURES
ii
Acknowledgement
The successful completion of this work would not have been possible without the
invaluable guidance and support of several individuals. We would like to express our
sincere gratitude to our professor, Dr. Vo Le Cuong, for his exceptional teaching and for
sharing his profound knowledge in this field. His inspiring teaching style and emphasis
on the importance of research have been instrumental in shaping our understanding and
approach to this project. We are particularly thankful for his meticulous feedback and
guidance during the report writing process.
Furthermore, we extend our appreciation to our Teaching Assistant, Mr. Nguyen
Trong Hung, for his remarkable friendliness and unwavering enthusiasm in supporting
us throughout the course. His comfortable and easy-to-understand teaching style has
made complex concepts accessible, and he has been one of the most impressive TAs we
have had the pleasure of learning from.
Finally, we would like to thank our classmates for their collaborative spirit and
support. Studying alongside such a dedicated and encouraging group has been a truly
enjoyable experience, and we value the camaraderie we have shared. We love studying
with them.
iii
Task Assignment
iv
Abstract
This report details the development and verification of an image processing system
implemented in Verilog Hardware Description Language (HDL). The project focuses
on demonstrating the fundamental principles of handling and manipulating image data
within a simulation environment using ModelSim. While not targeted for direct hard-
ware implementation, the system effectively models an image processing pipeline con-
sisting of image input, basic image enhancement, and image output stages. The image
input stage, simulated by a Verilog module, reads pre-processed hexadecimal image
data, mimicking the behavior of an image sensor by generating synchronized pixel
data and control signals. Image processing operations, including inversion, brightness
control, and thresholding, are implemented within the input module, selectable via
a parameter file. The image output stage utilizes a Verilog module to write the pro-
cessed pixel data, along with the necessary header information, into a standard Bitmap
(BMP) image file for visual verification. Simulation results demonstrate the functional
correctness of the implemented algorithms, showcasing the successful generation of
output images corresponding to each processing operation. This project highlights the
importance of simulation and modular design in the FPGA development flow for im-
age processing applications and provides a valuable foundation for understanding the
translation of image processing concepts into hardware descriptions. The limitations
of the current design for direct hardware synthesis, particularly regarding memory
management, are also discussed. Our code and relevant files are inspired from [1] [2]
1
1 Introduction
1.2 Motivation
Developing and verifying complex FPGA designs can be a time-intensive and chal-
lenging process. This project is motivated by the need to provide a clear and accessible
understanding of how to represent, process, and manipulate image data using Verilog,
specifically within a simulation environment. The focus on simulation allows for rapid
prototyping and validation of image processing algorithms, enabling designers to iter-
ate and refine their designs efficiently before committing to resource-intensive hardware
implementation. By concentrating on the design and compilation within ModelSim, this
work aims to offer valuable insights into the practical aspects of designing and verifying
image processing algorithms in Verilog.
1.3 Objectives
The primary goal of this project is to demonstrate the fundamental steps involved
in processing image data using Verilog HDL within a simulated FPGA environment.
The specific objectives include:
• Developing a Verilog-based workflow for reading image data from a file, simulating
2
the behavior of an image source.
• Designing the necessary control logic and data flow within the Verilog modules to
perform the image processing operations.
• Developing a Verilog module to write the processed image data back to a file for
visual verification of the results.
• By achieving these objectives, this project lays the groundwork for understanding
the design and verification of image processing algorithms in Verilog, providing a
solid foundation for future FPGA-based implementations.
3
2 Image Theory Review
This section reviews basic concepts in image theory, focusing on simple topics such
as image representations and basic operations. These fundamental ideas are straightfor-
ward, yet essential, for understanding and implementing more advanced image process-
ing techniques. By emphasizing simplicity, we aim to provide a clear starting point for
the project’s technical focus.
• Image Dimensions: Images have a width and a height, representing the number
of pixels horizontally and vertically, respectively. The code uses WIDTH and
HEIGHT parameters.
• Raster Scan Order: Image data is often processed and stored in a sequential manner,
typically row by row, from left to right. This is called raster scan order. You can see
this in the nested for loops in both the MATLAB and Verilog code. The MATLAB
code specifically handles the BMP’s bottom-up row order.
• Color Depth (Bit Depth): Determines the number of bits used to represent each
color component. The code mentions a 24-bit BMP image (RGB888). This means
8 bits are used for Red, 8 for Green, and 8 for Blue.
• Value Range: With 8 bits per component, each color component can have values
from 0 to 255. 0 represents the absence of that color, and 255 represents its maxi-
mum intensity. The code frequently checks for values exceeding 255 or going below
0 to prevent color overflow/underflow.
• Pixel Value: A single pixel in a 24-bit RGB image is represented by three values
(R, G, B), defining its color
4
• BMP Header: BMP files have a header that contains metadata about the image,
such as:
• Brightness Control
5
– Clipping: Important to prevent color values from going outside the valid 0-255
range. If a value exceeds 255, it’s clipped to 255; if it goes below 0, it’s clipped
to 0.
• Inversion (Negative)
– Concept: Converts an image into a binary (black and white) image based on a
6
threshold value. Pixels with intensity values above the threshold are set to one
color (usually white), and those below are set to another (usually black).
– Implementation: The code calculates the average intensity of the R, G, and B
components and compares it to the THRESHOLD parameter. If the average is
greater than the threshold, the pixel is set to white (255, 255, 255); otherwise,
it’s set to black (0, 0, 0).
– Thresholding
* Concept: Converts an image into a binary (black and white) image based
on a threshold value. Pixels with intensity values above the threshold are
set to one color (usually white), and those below are set to another (usually
black).
* Implementation: The code calculates the average intensity of the R, G, and
B components and compares it to the THRESHOLD parameter. If the av-
erage is greater than the threshold, the pixel is set to white (255, 255, 255);
otherwise, it’s set to black (0, 0, 0).
7
Figure 2.5 Image with threshold editing
• Why Hex? Verilog’s $readmemh system task is designed to read data from a text
file where values are represented in hexadecimal format. This is why the initial
BMP needs to be converted to hex. Each pair of hexadecimal digits represents one
byte (8 bits) of data.
8
3 Design and Implementation
This section details the design and implementation of the image processing system,
covering the system architecture and the individual Verilog modules developed for image
input, processing, and output.
Start
Input
.hex file
Output
.bmp file
End
The Image Reading module (image_read.v) is responsible for reading the pre-
processed hexadecimal representation of the input image from a file. It simulates the
behavior of an image sensor by providing synchronized pixel data along with horizon-
tal and vertical sync signals. The Image Processing stage is integrated within the im-
age_read.v module for this project. It performs the selected image enhancement opera-
9
tion (inversion, brightness control, or thresholding) on the incoming pixel data. Finally,
the Image Writing module (image_write.v) receives the processed pixel data and assem-
bles it, along with the necessary BMP header information, into an output bitmap image
file. The flow of data is unidirectional, moving sequentially through these three stages.
The image processing can also be demonstrated in the figure below:
Start
BMP Image
Hex File
Operation ?
End
10
3.2.1 Image Input (Reading Module - image_read.v)
The image_read.v module is designed to mimic the behavior of an image sensor,
providing a stream of pixel data synchronized with control signals.
Image Conversion (Matlab) As Verilog cannot directly interpret image files like
BMPs, a preliminary conversion step is essential. The provided Matlab script (im-
age_to_hex.m) performs this conversion. It reads the input image (BMP, PNG, or JPG)
using the imread function. The script then iterates through the image pixels, extracting
the Red, Green, and Blue (or grayscale) values. Crucially, the iteration proceeds from
the bottom row to the top row, mirroring the pixel storage order in BMP files. The ex-
tracted pixel values are then written to a .hex file in hexadecimal format, with each value
on a new line. This .hex file serves as the input for the image_read.v module.
11
Start Check Number of Channels
Display Success
Message
End
Explanation:
• The flowchart starts by checking if the input and output paths are provided.
• It reads the image file using imread, which handles different image formats.
12
• It gets the dimensions to iterate through pixels.
• Nested loops process pixels row by row (bottom to top) and column by column.
• It checks the number of color channels to handle RGB and grayscale images.
• Outputs: VSYNC (vertical sync pulse), HSYNC (horizontal sync pulse), DATA_R0,
DATA_G0, DATA_B0, DATA_R1, DATA_G1, DATA_B1 (RGB data for two par-
allel pixels), and ctrl_done (flag indicating completion).
Internally, the module utilizes several key signals and memory structures:
• total_memory: A large register array to store the entire image data read from the
.hex file using the $readmemh system task within an initial block.
• org_R, org_G, org_B: Integer arrays used to store the separated Red, Green, and
Blue color components of the image pixels in row-major order. The pixel data from
temp_BMP is reordered and separated into these arrays, accounting for the bottom-
up row order in the BMP format.
13
HSYNC Done
Start
Yes
Figure 3.9 Verilog Module for Reading and Processing Image Code Flow
Explanation
• The module starts by reading the hex file into memory during initialization.
• It waits for a start signal, typically triggered by the reset going high.
• During the DATA state, it calculates the memory address, reads pixel data, performs
the selected image processing, and outputs the processed data.
14
• Counters manage the row and column progression.
• The module is parameterized with WIDTH, HEIGHT, and INFILE, specifying the
image dimensions and the desired output file name. BMP_HEADER_NUM defines
the size of the BMP header (54 bytes).
• An initial block defines the BMP_header register array with the appropriate values
for a 24-bit uncompressed BMP image of the specified dimensions. These header
15
values include the file signature, file size, image dimensions, bits per pixel, and
other relevant information as described in Section 2.1.
• Another initial block opens the output file (INFILE) in write-binary mode using the
$fopen system task.
• An always block, triggered by the Write_Done signal, writes the BMP header to
the file using $fwrite, followed by the processed pixel data. The pixel data is writ-
ten in BGR order, consistent with the BMP format. The loop iterates through the
processed pixel data, writing the RGB components for the two parallel pixels.
Start
End
Explanation:
• It waits for a Write_Done signal from the processing module (in this case, the im-
age_read module).
16
• It then iterates through the processed pixel data and writes it to the file in the correct
BMP format (BGR order).
Start
End
17
Explanation
• It generates the necessary clock and reset signals for the design.
• The simulation runs for a predetermined time period, allowing the modules to pro-
cess the image.
18
4 Experimental Results
19
Figure 4.13 Image with lower brightness
With lower brightness operation, the values in each channel R, G and B are reduced
(in this simulation, they are all reduced by 100). So it is expectable to see in the output
waveform, almost all data will have small values. So the output data can be considered
accurate.
20
Figure 4.15 Image with higher brightness
Similarly, the values in each channel R, G, B are increased with the higher bright-
ness adjustment. The simulation has returned the results with value mostly larger than
128. It should be an accurate result.
21
Figure 4.17 Image with color inversion
In this operation, when looking at the waveform, we can see similar values in the
data of all three channels. That is because our code has processed the image in two
steps: convert the image into grayscale and then invert the color. Grayscale conversion
will result in the similar in all three value of R, G and B in a pixel. That should explain
the values in the waveform.
22
Figure 4.19 Image with threshold editing
Lastly, with the threshold operation, the image will be turned into a black-and-
white image. With the previous explanation, we know that a white pixel contains value
255, alongside with a black pixel contains value 0 in all three channels RGB. That should
bring a clear explanation for how all the data of a pixel is similar in the threshold wave-
form, and all of them are either 00000000 (value 0) or 11111111 (value 255).
4.3 Discussion
The simulation results, showcasing the successful generation of output images for
inversion, brightness control, and thresholding, strongly indicate the functional correct-
ness of the implemented Verilog modules. By visually comparing the output images
23
with the expected outcomes for each processing operation, we can confirm that the algo-
rithms were accurately translated into hardware description language. The inverted im-
age clearly demonstrates the reversal of pixel intensities, while the brightness-adjusted
images show the intended shift in overall luminance. The thresholded image effectively
segments the image into foreground and background based on the defined threshold
value.
The functionality of the image_read module as a model for an image sensor is a
key aspect of this project. The successful generation of HSYNC and VSYNC signals,
along with the sequential output of pixel data, mimics the behavior of a real-world image
sensor. This approach proves valuable for functional verification, allowing designers to
test downstream image processing logic in a simulated environment before integrating
with actual hardware. The ability to control parameters like HSYNC_DELAY allows for
simulating different sensor characteristics and testing the robustness of the processing
pipeline to timing variations.
The image_write module serves as an equally crucial component for verification.
By writing the processed pixel data into a standard BMP format, it enables a direct visual
assessment of the processing results. This is significantly more intuitive and efficient
than analyzing raw numerical data from the simulation waveforms. The correct imple-
mentation of the BMP header ensures that the output image is rendered accurately by
standard image viewers. This capability greatly simplifies the debugging and validation
process for image processing algorithms.
Several important considerations arise from the design and implementation of this
project. Firstly, the current implementation, while effective for simulation, relies on
large memory arrays to store the entire image. This approach is not directly synthe-
sizable for efficient hardware implementation on an FPGA. For real-world FPGA de-
ployments, these memory arrays would need to be replaced with Block RAM (BRAM)
resources, requiring the design of address generators and memory controllers to manage
data access.
Secondly, the parallel processing of two pixels, while intended to speed up simu-
lation, introduces a level of complexity in the indexing and data handling. Depending
on the target FPGA architecture and the complexity of the processing algorithm, the
degree of parallelism can be further optimized for improved hardware performance and
resource utilization.
Finally, while this project focuses on basic image enhancement operations, the
modular design allows for the integration of more complex algorithms. The image_read
module can be seen as a generic data source, and the processing logic within it can
be replaced or expanded to implement a wide range of image processing tasks. The
24
image_write module provides a consistent mechanism for verifying the output of any
such processing pipeline.
In conclusion, this project successfully demonstrates the fundamental principles of
image processing using Verilog HDL within a simulation environment. The modular
design and the inclusion of image reading and writing modules provide a comprehen-
sive framework for developing and verifying image processing algorithms. While direct
hardware synthesis requires further design considerations, this work provides a valuable
foundation for understanding the translation of image processing concepts into hardware
descriptions and highlights the importance of simulation and verification in the FPGA
design flow.
25
5 Conclusion
26
REFERENCE
[1] V. L. Le, “Image processing on FPGA using Verilog HDL.” [Online]. Available:
https://www.fpga4student.com/2016/11/image-processing-on-fpga-verilog.html
27