SpinalHDL | Scala based HDL
kandi X-RAY | SpinalHDL Summary
kandi X-RAY | SpinalHDL Summary
Scala based HDL
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SpinalHDL
SpinalHDL Key Features
SpinalHDL Examples and Code Snippets
Community Discussions
Trending Discussions on SpinalHDL
QUESTION
I am trying to do something similar to
printf("My age is: %d\n", age);
but I do not have access to the standard libraries. Instead, the print function I do have access to, print()
, takes only a const char*
.
I am not allowed to use sprintf
or itoa
or anything of that sort because that is simply not available. Is there a way I can print the number?
P.S. If you need more context, https://github.com/SpinalHDL/VexRiscv/issues/215 it is :)
Thanks in advance
...ANSWER
Answered 2021-Oct-31 at 11:59It is kind of clumsy. You can get digits starting from the low end, using % 10 (modulus) to isolate the units digit and / 10 (integer divide) to shift the number to the right. You do that in a loop until the int goes down to zero. If the whole thing is zero, you have to set the '0' char yourself, because the loop will not then do the first iteration.
You need to add '0' to each digit to make it an ASCII digit, and you need to store the successive digits in a char array.
You need to append a NUL ('\0') char on the end as a string terminator.
Then you need to reverse the whole string, because the digits came out in reverse order. Alternatively, you can fill the char array from the end, but then you have to copy the whole string (including the NUL) up to the front of the buffer.
If the integer can be negative, you need to remember that, make it positive by subtracting it from zero, and stick a '-' on the end before you reverse it.
Sounds a lot, but the long2str function takes about 20 lines.
Edit: there is a recursive solution too. Going down the required depth, and saving the digits on the way back out, avoids the reverse sequence issue (including the minus sign), and creates the string without padding.
QUESTION
I would like to make a ternary condition in SpinalHDL as a ternary assignment in Verilog:
e.g.
...ANSWER
Answered 2020-Aug-15 at 16:51I just saw it is possible to use:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SpinalHDL
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page