Printing characters to LCD - Verilog HDL - printing

I have a question regarding printing character to an LCD screen.
I am using an Altera DE1-SoC 5CSEMA5F31C6N and a LT24 Terasic LCD.
I have a question regarding printing letters in a row on the LCD.
I am relying on the x and y counter to raster across the screen, starting at (0,0) upper left corner of the screen.
Incrementing x all the way to the end of the row, and once reached the end, reset x to 0, increment the y and then continue counting x again until the end of the screen
LCD pixels
i am creating arrays to print character values (8x8 px)
The long array concatenates 'each row of pixels' for all characters, and then whilst the counter is rastered across the screen the pixels for each character will print to the lcd.
eg.
row0 - print first row of pixels for all characters.
row1 - print second row of pixels for all characters.
however, when i try to print 2 characters, the order of the characters being printed is reversed [from the origin (0,0)]
e.g. if i want to print 'I' then 'M'. I actually get 'M' and 'I' in that order.
when i try to print 3 characters, then no characters show at all!
I am really struggling to understand why this is the case, as the counter values are used to test the current bit of the character and then draw the pixel
Any help would be appreciated.
I have basic verilog understanding, but comfortable with c programming
Thank you,
code snippets are below.
////////////////////////////// code ////////////////////////////////////////////
reg [0] totalCharData [23:0]; // pixel row0
reg [1] totalCharData [23:0]; // pixel row1
reg [2] totalCharData [23:0]; // pixel row2
reg [3] totalCharData [23:0]; // pixel row3
reg [4] totalCharData [23:0]; //
reg [5] totalCharData [23:0]; //
reg [6] totalCharData [23:0]; //
reg [7] totalCharData [23:0]; // pixel row 7
// character ‘I'
totalCharData[7][7:0] = 8'b11111111;
totalCharData[6][7:0] = 8'b11111111;
totalCharData[5][7:0] = 8'b00111100;
totalCharData[4][7:0] = 8'b00111100;
totalCharData[3][7:0] = 8'b00111100;
totalCharData[2][7:0] = 8'b00111100;
totalCharData[1][7:0] = 8'b11111111;
totalCharData[0][7:0] = 8'b11111111;
// character ‘M'
totalCharData[7][15:8] = 8'b11100111;
totalCharData[6][15:8] = 8'b11101111;
totalCharData[5][15:8] = 8'b11111111;
totalCharData[4][15:8] = 8'b11111111;
totalCharData[3][15:8] = 8'b11010011;
totalCharData[2][15:8] = 8'b11000011;
totalCharData[1][15:8] = 8'b11000011;
totalCharData[0][15:8] = 8'b11000011;
// character ‘E'
totalCharData[7][23:16] = 8'b11111111;
totalCharData[6][23:16] = 8'b11111111;
totalCharData[5][23:16] = 8'b11100000;
totalCharData[4][23:16] = 8'b11111111;
totalCharData[3][23:16] = 8'b11111111;
totalCharData[2][23:16] = 8'b11100000;
totalCharData[1][23:16] = 8'b11111111;
totalCharData[0][23:16] = 8’b11111111;
// X Counter
always # (posedge clock or posedge resetApp) begin
if (resetApp) begin
xAddr <= 8'b0;
end else if (pixelReady) begin
if (xAddr < (WIDTH-1)) begin
xAddr <= xAddr + 8'd1;
end else begin
xAddr <= 8'b0;
end
end
end
// Y Counter
always # (posedge clock or posedge resetApp) begin
if (resetApp) begin
yAddr <= 9'b0;
end else if (pixelReady && (xAddr == (WIDTH-1))) begin
if (yAddr < (HEIGHT-1)) begin
yAddr <= yAddr + 9'd1;
end else begin
yAddr <= 9'b0;
end
end
end
// draw characters to the lcd
always # (posedge clock or posedge resetApp) begin
if (resetApp) begin
pixelData[15:0] <= 16'h0000; ; // wipe the full screen with background
end else begin // whilst bitton held, make blue
if ((xAddr>=0) && (xAddr<24) && (yAddr>=0) && (yAddr<8))begin // draw complete row of pixels for all the characters in line
if ((totalCharData[yAddr][xAddr] == 1'b1))begin // test the current bit using the counters
pixelData[15:0] <= 16'hFFE0; // yellow - draw pixel if the current bit is 1 as defined
end
else begin
pixelData[15:0] <= 16'h0000;
end // else
end else begin
pixelData[15:0] <= 16'h0000; // black screen
end
end
end

I would look at how you initially define your registers. Based on how you are accessing them in your RTL, I expected them be defined as
reg [23:0] total_char_data [0:7]
This does in one line what you were trying to do in 8, BUT notice that the [23:0] is on the left side (packed array) and the [0:7] is on the right (unpacked array) in my definition. When done like this, I think your existing assignments work.
Based on this though, I don't know how you are even getting two characters to show up. I would have imagined just looking at it, that only one character would appear, within the 8x8 window. I don't suppose you have any interesting WARNINGS in your compile that say things like, "outside of bit range"?

Related

Trying to write a recursive program that reveals all adjacent tiles that are blank

I'm trying to make a clone of minesweeper. In that game, there is a feature that, whenever you click an empty box, all adjacent empty tiles are revealed and empty tiles adjacent to those empty tiles are also revealed.
Right now when I tried to implement this, it only reveals the 8 adjacent tiles of the tile I clicked, not any other empty tiles which are next to the empty tiles revealed
Here is the code that I'm running right now (it has 2 parameters row and col):
local rowCords = {row-16, row, row+16}
local colCords = {col-16, col, col+16}
--Check surroundings
for r = 1, 3, 1 do
for c = 1, 3, 1 do
curRow = rowCords[r]
curCol = colCords[c]
if (curRow >= 16 and curRow <= 400 and curCol >= 16 and curCol <= 176) then
if boardCords[Board:getBox(curRow, curCol)].value == 1 then
boardCords[Board:getBox(curRow, curCol)].revealed = true
end
end
end
end
In your algorithm you are checking only 9 tiles, but you must do this recursively for checked tiles. So your algorithm must be like:
function revealTile(row, col)
if (row >= 16 and row <= 400 and col >= 16 and col <= 176) then
local tile = boardCords[Board:getBox(row, col)]
-- Try to reveal tile
if not tile.revealed and tile.value == 1 then
tile.revealed = true
-- Try to reveal surroundings
for r = row-16,row+16,16 do
for c = col-16,col+16,16 do
revealTile(r, c)
end
end
end
end
end

Can anybody please tell me how to set or reset a bit in lua..?

I want to perform set and reset of particular bit in a number. As I'm using lua 5.1 I can't able to use APIs and shifting operators so it is becoming more and more complex so please help me finding this
bit library is shipped with the firmware.
Read the documentation: https://nodemcu.readthedocs.io/en/release/modules/bit/
You can do it without external libraries, if you know the position of the bit you wish to flip.
#! /usr/bin/env lua
local hex = 0xFF
local maxPos = 7
local function toggle( num, pos )
if pos < 0 or pos > maxPos then print( 'pick a valid pos, 0-' ..maxPos )
else
local bits = {} -- populate emtpy table
for i=1, maxPos do bits[i] = false end
for i = maxPos, pos +1, -1 do -- temporarily throw out the high bits
if num >= 2 ^i then
num = num -2 ^i
bits [i +1] = true
end
end
if num >= 2 ^pos then num = num -2 ^pos -- flip desired bit
else num = num +2 ^pos
end
for i = 1, #bits do -- add those high bits back in
if bits[i] then num = num +2 ^(i -1) end
end
end ; print( 'current value:', num )
return num
end
original value: 255
current value: 127
pick a valid pos, 0-7
current value: 127
current value: 255

Minimum Change Maker Returning Optimal Solution and No Solution

I need Help adding a if clause to my Change Maker, so that if say I have denominations of coins that can't equal the input coin value. For Example I have Coins worth 2,4,6 and I have a Value of 1. I Want it to return Change Not Possible I tried adding a clause to it below but when I test it I get 1.#INF
I also am curious how I can find the optimal coin solution, So on top of saying the minimum number of coins it returns the optimal coin setup if there is one.
function ChangeMaking(D,n)
--[[
//Applies dynamic programming to find the minimum number of coins
//of denominations d1< d2 < . . . < dm where d1 = 1 that add up to a
//given amount n
//Input: Positive integer n and array D[1..m] of increasing positive
// integers indicating the coin denominations where D[1]= 1
//Output: The minimum number of coins that add up to n
]]
F = {} -- F is List Array of Coins
m = tablelength(D)
F[0] = 0
for i =1,n do
temp = math.huge
j = 1
while j <= m and i >= D[j] do
temp = math.min(F[ i - D[j] ], temp)
j = j + 1
end
F[i] = temp + 1
end
--I wanted to Catch the failed Solution here but I return 1.#INF instead
--if F[n] <= 0 and F[n] == 1.#INF then print("No Change Possible") return end
return F[n]
end
function main()
--[[
//Prints a Greeting, Asks for Denominations separated by spaces.
// Iterates through the input and assigns values to table
// Table is then input into ChangeMaker, and a while loop takes an n value for user input.
// User Enters 0 to end the Loop
]]
io.write("Hello Welcome the to Change Maker - LUA Edition\nEnter a series of change denominations, separated by spaces: ")
input = io.read()
deno = {}
for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end
local i = 1
while i ~= 0 do
io.write("Please Enter Total for Change Maker, When Finished Enter 0 to Exit: ")
input2 = io.read("*n")
if input2 ~= 0 then io.write("\nMinimum # of Coins: " .. ChangeMaking(deno,input2).."\n") end
if input2 == 0 then i=0 print("0 Entered, Exiting Change Maker") end
end
end
function tablelength(T)
--[[
//Function for grabbing the total length of a table.
]]
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
main()

How to export/convert line projection to excel table and order the Y coornidate

I wrote a code that can get line projection (intensity profile) of an image, and I would like to convert/export this line projection (intensity profile) to excel table, and then order all the Y coordinate. For example, except the maximum and minimum values of all the Y coordinate, I would like to know largest 5 coordinate value and smallest coordinate value.
Is there any code can reach this function? Thanks,
image line_projection
Realimage imgexmp
imgexmp := GetFrontImage()
number samples = 256, xscale, yscale, xsize, ysize
GetSize( imgexmp, xsize, ysize )
line_projection := CreateFloatImage( "line projection", Xsize, 1 )
line_projection = 0
line_projection[icol,0] += imgexmp
line_projection /= samples
ShowImage( line_projection )
Finding a 'sorted' list of values
If you need to sort though large lists of values (i.e. large images) the following might not be very sufficient. However, if your aim is to get the "x highest" values with a relatively small number of X, then the following code is just fine:
number nFind = 10
image test := GetFrontImage().ImageClone()
Result( "\n\n" + nFind + " highest values:\n" )
number x,y,v
For( number i=0; i<nFind; i++ )
{
v = max(test,x,y)
Result( "\t" + v + " at " + x + "\n" )
test[x,y] = - Infinity()
}
Working with a copy and subsequently "removing" the maximum value by changing that pixel value. The max command is fast - even for large images -, but the for-loop iteration and setting of individual pixels is slow. Hence this script is too slow for a complete 'sorting' of the data if it is big, but it can quickly get you the n 'highest' values.
This is a non-coding answer:
If you havea LinePlot display in DigitalMicrograph, you can simply copy-paste that into Excel to get the numbers.
i.e. with the LinePlot image front most, preses CTRL + C to copy
(make sure there are no ROIs on it).
Switch to Excel and press CTRL + V. Done.
==>

My stack (LIFO) memory overflows and prevents any further reading of memory

I've been working on coding a simple stack memory. It has 4 address bits and thus can store 16 elements. Everything works fine, but the problem is that when all 16 memory elements have been written to, the counter that keeps track of the memory location overflows and resets it to 0000. I cannot find out the reason for this. All my registers are of correct width.
reg_push and reg_pop are incremented and decremented together, and these are the registers that keep track of the memory location.
Here is the simulation showing the overflow.
Here is the code:
module stack # (parameter dbits = 3, abits = 4)(
input clock,
input reset,
input push,
input pop,
input [dbits-1:0] din,
output [dbits-1:0] dout,
output full,
output empty
);
reg [dbits-1:0] regarray[2**abits-1:0]; //number of words in fifo = 2^(number of address bits)
reg [abits-1:0] reg_push, reg_pop, next_push, next_pop;
reg full_reg, empty_reg, full_next, empty_next;
reg [dbits-1:0] out;
wire wr_en;
wire db_push, db_pop;
reg dffpop1, dffpop2, dffpush1, dffpush2;
always # (posedge clock) dffpush1 <= push;
always # (posedge clock) dffpush2 <= dffpush1;
assign db_push = ~dffpush2 & dffpush1; //monostable multivibrator to detect only one pulse of the button
always # (posedge clock) dffpop1 <= pop;
always # (posedge clock) dffpop2 <= dffpop1;
assign db_pop = ~dffpop2 & dffpop1; //monostable multivibrator to detect only one pulse of the button
assign wr_en = db_push & ~full; //only push if write signal is high and stack is not full
//always block for write operation
always # (posedge clock)
if(wr_en) regarray[reg_push] = din;
//always block for read operation
always # (posedge clock)
begin
if(db_pop)
out <= regarray[reg_pop];
end
always # (posedge clock or posedge reset)
begin
if(reset)
begin
full_reg <= 0;
empty_reg <= 1;
reg_push <= 0;
reg_pop <= 0;
end
else
begin
full_reg <= full_next;//created the next registers to avoid the error of mixing blocking and non blocking assignment to the same signal
empty_reg <= empty_next;
reg_push <= next_push;
reg_pop <= next_pop;
end
end
always # (*)
begin
full_next = full_reg; //default values stay the same
empty_next = empty_reg;
next_push = reg_push;
next_pop = reg_pop;
if(db_push)
begin
if(~full) //if stack is not full continue
begin
empty_next = 0;
next_push = reg_push + 1;
next_pop = reg_pop + 1;
if(reg_push == (2**abits - 1)) full_next = 1; //all registers have been written to
end
end
else if (db_pop)
begin
if(~empty) //if stack is not empty continue
begin
full_next = 0;
next_pop = reg_pop - 1;
next_push = reg_push - 1;
if(reg_pop == 0) empty_next = 1; //all data has been read
end
end
end
assign full = full_reg;
assign empty = empty_reg;
assign dout = out;
endmodule
Now if I use this stack without making it reach its full capacity, it will work perfectly. It's only when I store all 16 elements into it that the problem arises.
Extend your pop pointer an extra bit.
A 4-bit register can only store the a value 0 through 15. Any value above that will ignore the upper bits, effectively doing a mod 16. Hence assigning 16 will result in 0.
Option 1: expand to a 5-bit register:
Try changing:
reg [abits-1:0] reg_push, reg_pop, next_push, next_pop;
To:
reg [abits:0] reg_push, reg_pop, next_push, next_pop;
Option 2: Use full_reg are the 5th bit in evaluations:
Change:
if(reg_push == (2**abits - 1)) full_next = 1; //all registers have been written to
...
if(reg_pop == 0) empty_next = 1; //all data has been read
To:
if({full_reg,reg_push} >= (2**abits - 1)) full_next = 1; //all registers have been written to
...
if({full_reg,reg_pop} == 0) empty_next = 1; //all data has been read

Resources