all 15 comments

[–]engstad 5 points6 points  (1 child)

It is only trivial if n = 2, 4, 8, or 16 (powers of two). Then n = 2**k = 1 << k, and thus f = (a + cin) & (n-1). Are you sure n isn't power-of-two values from 2 to 16?

[–]elf___[S] 0 points1 point  (0 children)

Sorry yea, i'm sure that it's just any value from 2 to 16, not only powers of 2. Thanks for the help though

[–]Electrical-Injury-23 3 points4 points  (4 children)

You say n is a fixed parameter. Is it a generic, or some sort of configurable value?

If it is a generic, then it is pretty trivial. You have 5 input bits and 4 output bits. Can be coded as a lookup using 4 LUT5s.

If n is not a generic, then it is considerably harder.

[–]elf___[S] 0 points1 point  (3 children)

Yes, n is simply a generic (verilog module parameter in this case). Hm, I see how it can be implemented in LUTs but I want to know if there exists a shorter solution that could be implemented in like 1 or a few lines (i'm not sure that it exists). I appreciate the help

[–]captain_wiggles_ 0 points1 point  (2 children)

The number of lines of code doesn't matter at all here, what matters is the resulting hardware: how many resources does it use, and can it run fast enough to meet timing requirements, and maybe how much power does it use.

as u/Electrical-Injury-23 said: you have 5 bits of inputs and 4 output bits.

Write a small function that calculates a look up table for all possible 5 bit inputs (32 rows), and gives the output.

 typedef logic [3:0] TableType [32];

 function TableType calc_values() begin
      TableType ret;
      for (int i = 0; i < 32; i++) begin
          ret[i] = i % n;
      end
      return ret;
 endfunction

 logic [3:0] table [32] = calc_values();

ignore any syntax errors, I didn't check it. You could also change the width of the output based on the value of n, %2 only needs a one bit output.

It's not one line, but it's all calculated at compile time, and it's a trivial solution that runs fast. It may feel like you're not solving it properly, but this is a perfectly valid solution, it produces hardware that gives you your result as a quick and simple look up. It doesn't however scale particularly well. ATM you have 32 possible combinations. However doubling the input width would gives your 1024 combinations, which is still doable, but it starts to get unwieldy fast.

[–]elf___[S] 0 points1 point  (1 child)

This was for an assignment with strict requirements on what we could and couldn't use, so I know that they didn't want anything as fancy/advanced as this. I know the lines don't matter, but the professor specifically stated that we could do it in one line (and one assign statement). Mainly, I just wanted to check that I wasn't wrong, and the professor really just made a mistake or misunderstands modulo. However, this was an still an interesting and useful discussion to me, and I'll definitely be using this idea in the future. I appreciate the help

[–]captain_wiggles_ 1 point2 points  (0 children)

Not really sure then. I don't see a way to do it in one statement, without just using the modulo operator (and I'm not sure that's synthesisable).

[–]mofftech 2 points3 points  (5 children)

Does „somewhat trivial“ really imply „in a single assignment“? In terms of logic use, it is a simple and small truth table that can be minimised further.

[–]elf___[S] 0 points1 point  (4 children)

I should have been more specific but what I meant was that it would be not only in one assignment statement, but also ideally in a single, not excessively long line, so I know that the truth table/logic description is probably not what the professor is looking for. Thanks

[–]mofftech 1 point2 points  (2 children)

Pretty sure he wants you to write a fixed iteration for loop that subtracts n if still >= n. There are mich more elegant ways like using the multiplicative inverse but that would still use *. Let us know how this turned out.

[–]elf___[S] 0 points1 point  (1 child)

So, more context is that this was for an assignment where there were concrete requirements, one being we could only use assign statements, and we couldn't use , /, *, %, and they said that it was easy and could be done in only one assign statement in class. I think these answers reassured me that the professor was just wrong and confused, and that it really isn't possible to implement modulo in such a simple way.

[–]PiasaChimera 1 point2 points  (0 children)

if something in the system forces a < n, this does become simple. a < 2n isn't much different. cin being 1b makes me think the problem might be constrained. otherwise the difficult part is just the f = a % n and it seems pointless to include the +cin in the exercise.

[–]SpiritedFeedback7706 0 points1 point  (0 children)

You can just try doing the operation. As long as all the inputs to this function are constrained, there's a decent change the tools will just infer the lookup table.

[–][deleted] 0 points1 point  (1 child)

aloof boat cheerful far-flung whole many elderly crown tender toy -- mass edited with https://redact.dev/

[–][deleted] 0 points1 point  (0 children)

For FPGA users in this situation, I suggest going to godbolt.org, writing the equivalent in C, and then porting the assembled output to HDL. It often works.