Are there Simulink guidelines for choosing ufix1 vs. boolean?

In the past I've tried making all my 1 bit signals in my Simulink models ufix1 or all of them boolean and neither option seemed to work perfectly. I haven't yet found guidelines from MathWorks for choosing the best type in a particular case - are there any? Or perhaps you really can make them all one type or the other, but I did it wrong.
From my perspective, I don't see the need for a ufix1 data type - does anybody know the reason?

4 Comments

Hi Dara, thanks for taking the time to contribute to MATLAB Central. Generally speaking, if the signal is used for only control logic, then Boolean may be preferred. If the signal is used for driving arithmetic blocks, then ufix1 is preferred. Can you elaborate on what kind of issues you ran into with either MATLAB function blocks, or with Simulink blocks? Best Regards, Robert Anderson
Thanks Robert,
I tried making everything ufix1 recently and that doesn't work since when I use an enabled register in HDL coder where the enable is fed with a ufix1 signal, then the model will simulate fine, but not write out HDL code:
Error: The 'Reset' port for the block 'er_231' must be a scalar boolean type for HDL code generation.
So now I'll try using boolean for everything and see where that breaks down (I think it does, but I can't recall how). I do think this adds unnecessary confusion to the user. A one bit signal is a one bit signal - I don't see why there needs to be two possible characterizations of it.
I have entered an enhancement request to remove this restriction on enable port. We hope to remove this limitation soon.
Thank you. I see that HDL code writes out now when ufix1 goes into an enable port (on 2017b). I will reply here if I see any other issues with using ufix1 over Boolean.
Per Tim McBrayer's answer, I can see how there might be issues forcing all 1 bit signals into the Boolean category which was my original guess. But forcing them all into ufix1 works just as well for me (if it works in HDL coder) and is consistent with my manager's preference to use ufix8 instead of uint8 and similar (because certain methods that work for fi objects don't work for the special categories like unit8, int16, etc.)

Sign in to comment.

 Accepted Answer

A ufix1 type is an arithmetic type--it represents a number. You can add, subtract, etc., with it. It can represent the numeric values 0 and 1. A boolean type a not an arithmetic type (it stores true and false) and doesn't support arithmetic operations. VHDL in particular is strongly typed, and upholds this distinction in its language implementation. HDL Coder in general follows the most restrictive commonalities between VHDL and Verilog, so it honors the VHDL distinction between types.
Simulink and HDL Coder go even further with ufix1, as they support binary point scaling. For example, ufix1_En2 can store either 0 or 0.25, in a single bit. ufix1_E4 can store 0 or 16, also in one bit. And so on. Arithmetic will be performed on these types in bit-true and cycle-accurate form. This is simply the nature of the full-featured fixed-point numeric system that MATLAB and friends support.

1 Comment

Thanks Tim,
I just have to get my head around where to use each type. I often have 1 bit signals that are used in both roles (i.e. the enable line of a register and adding or multiplying the signal against another). There seem to be many circumstances where the result is the same whether I use ufix1 or boolean (e.g. select line for a two input switch) and now I know not to use ufix1 in certain places. I think I have done some arithmetic with boolean but maybe I never wrote out code.
I'll probably just use boolean for all my one bit signals at the top level I/O and use a convert block to ufix1 as needed when I get an error.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!