Decimal points to Hexadecimal Conversion in Verilog - hex

I am working on a verilog code. I am completely new to verilog.
Suppose, I have to represent 0.6072529350088814 in hexadecimal, its value comes out to be 32'h26dd3b6a, as shown in code on the below :
https://electronics.stackexchange.com/questions/354594/cordic-algorithm-using-verilog
But I donot understand how this conversion works?
Suppose I want to display the value and see it in decimal, what should be code?
`timescale 1ns / 1ps
module top(
);
wire signed[31:0] init_value= 32'h26dd3b6a; // 0.6072529350088814
always#(*)
begin
$display("The init_value= %d", init_value);
end
endmodule
I have tested the above line of code, but this does not work.
The value of the init_value is displayed as
The init_value= 652032874
I expect that result should be 0.6072529350088814.

Code:
$display("The init_value = %f", $itor(init_value) / (2 ** 30));
Prints:
The init_value = 0.607253
I am not sure if the code under the link is correct. Theoretically, the conversion should be done through 2 ** 31 or 2 ** 32.

Related

Scilab - gui - many unknown variables error messages

Still trying to understand the logic of Scilab, I created a small calculation tool for a mechanical element. The main problem I have is finding the right order (or syntax) for the calculation code... I get a lot of "unknown variable" errors and I don't understand why?
I tried to change the order of definitions for the functions, declare the variables as global, etc. but nothing seems to help.
The code for the calculation is not long and also not complicated, but the gui was built using guibuilder, so the uicontrols definitions are probably much longer than they need to be.
Could somebody help me make this code working, as I would learn and understand a lot by this example, althought it contains more than one "problem zones"?
Here what I've done:
G = 78500;
table_titles = ["" "Wire diameter" "Wp" "Tau alwd" "M alwd" "Angle alwd"];
f=figure('figure_position',[910,163],'figure_size',
[903,537],'auto_resize','on','background',[33],'figure_name','Graphic
window number %d');
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')
handles.dummy = 0;
handles.sl_dwire=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',
[],'Max',[12],'Min',[0],'Position',
[0.0058208,0.77875,0.124375,0.06875],'Relief','default','SliderStep',
[0.1,1],'Style','slider','String',"Wire diameter",'Value',
[6],'VerticalAlignment','middle','Visible','on','Tag','sl_dwire',
'Callback','sl_dwire_callback(handles)')
handles.ed_dwire=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','off','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',
[],'Max',[1],'Min',[0],
'Position',[0.0058208,0.71875,0.124375,0.06875],'Relief',
'default','SliderStep',[0.01,0.1],'String',"wire diameter: " +
msprintf('%2.1f',handles.sl_dwire.Value) + "mm",'Style','text',
'Value',[0],'VerticalAlignment','middle','Visible','on','Tag',
'ed_dwire','Callback','auto')
handles.sl_wangle=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[180],'Min',[5],'Position',
[0.0090625,0.5191667,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.1,1],'String','Working angle','Style','slider','Value',
[50],'VerticalAlignment','middle','Visible','on','Tag','sl_wangle',
'Callback','sl_wangle_callback(handles)')
handles.ed_wangle=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','off','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0090625,0.4591667,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Working angle: " +
msprintf('%2.1f',handles.sl_wangle.Value) + "°",'Style','text',
'Value',[0],'VerticalAlignment','middle','Visible','on','Tag',
'ed_wangle','Callback','auto')
handles.sl_activel=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','on','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1000],'Min',[10],'Position',
[0.0090625,0.365,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.1,1],'String','Active length' ,'Style','slider',
'Value',[10],'VerticalAlignment','middle','Visible','on','Tag',
'sl_activel','Callback','sl_activel_callback(handles)')
handles.ed_activel=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','off','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0090625,0.305,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.01,0.1],'String','Active length: ' +
msprintf('%2.1f',handles.sl_activel.Value) +
"mm",'Style','text','Value',[0],'VerticalAlignment','middle','Visible',
'on','Tag','ed_activel','Callback','auto')
handles.ax_graph= newaxes();handles.ax_graph.margins = [ 0 0 0 0];
handles.ax_graph.axes_bounds = [0.4274266,0.0619266,0.3995485,0.5191743];
handles.tab_param=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','on','FontAngle','normal','FontName',
'Tahoma','FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],
'Position',[0.4308126,0.1690826,0.3950339,0.2178899],'Relief',
'default','SliderStep',[0.01,0.1],'String',string(table_param),'Style',
'table','Value',[0],'VerticalAlignment','middle','Visible',
'on','Tag','tab_param','Callback','tab_param_callback(handles)')
handles.sl_sfactor=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','on','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],
'Position',[0.0058208,0.6525688,0.124375,0.06875],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Safety factor",'Style',
'slider','Value',[0.58],'VerticalAlignment','middle','Visible','on',
'Tag','ed_sfactor','Callback','sl_sfactor_callback(handles)')
handles.ed_sfactor=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','off','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment',
'left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0058208,0.5925688,0.124375,0.06875],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Safety factor : " +
msprintf('%2.1f',handles.sl_sfactor.Value),'Style',
'text','Value',[0.58],'VerticalAlignment','middle','Visible','on',
'Tag','ed_dwire','Callback','auto')
handles.popm_wtype=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','on','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0058208,0.8618349,0.124375,0.0639450],'Relief','default',
'SliderStep',[0.01,0.1],'String',gettext("SL/DL|SM/DM|SH/DH"),'Style',
'popupmenu','Value',[2],'VerticalAlignment','middle','Visible','on',
'Tag','popm_wtype','Callback','popm_wtype_callback(handles)')
//////////
// Callbacks are defined as below. Please do not delete the comments
as it will be used in coming version
//////////
function sl_sfactor_callback(handles)
sf=handles.sl_sfactor.Value;
handles.ed_sfactor.String="Safety factor: " + msprintf('%3.2f',sf);
endfunction
function sl_dwire_callback(handles)
wd=handles.sl_dwire.Value;
Wp = %pi*wd^3/16;
Ip =%pi*wd^4/32;
Kt = G*%pi*Ip/(180*L);
Talwd = sf * calcform;
Malwd = Wp * Talwd;
alphaalwd = Malwd / Kt;
x=0:0.1:alphaalwd*1.5;
plot(x,Kt*x);
handles.ed_dwire.String="Wire diameter: " +
msprintf('%2.1f',wd) + "mm";
endfunction
function popm_wtype_callback(handles)
//Write your callback for popm_wtype here
if selected == 1 then
calcform =(1845 - 700*log10(wd));
elseif selected == 2 then
calcform =(2105 - 780*log10(wd));
elseif selected == 3 then
calcform = (2220 - 820*log10(wd));
end
endfunction
function sl_wangle_callback(handles)
handles.ed_wangle.String="Working angle: " +
msprintf('%2.1f',handles.sl_wangle.Value) + "°";
endfunction
function sl_activel_callback(handles)
//Write your callback for sl_activel here
handles.ed_activel.String="Active length: " +
msprintf('%2.1f',handles.sl_activel.Value) + "mm";
L=handles.sl_activel.Value;
endfunction
function tab_param_callback(handles)
//Write your callback for tab_param here
table_values = string([ wd Wp Talwd Malwd alphaalwd]);
table_param = [table_titles; [table_values]];
endfunction
I expect the code to dynamically update the graph and the parameters table according to the positions of input sliders and popup-menu.
Again, it would be very helpful if somebody could help me get this code working, as I would get answers for a lot of my questions concerning programming with scilab.
Thank you very much in advance!
First of all, your code is not directly executable, caused by the linebreaks. Please add ... after each line of a statement. This improves also the readability.
As mentioned by #luispauloml, you try to use variables, which exist just inside of another function. For example you try to reach table_param, which is just alive in tab_param_callback(handles).
To get rid of this, you have to define the output for the function:
function [table_values, table_param] = tab_param_callback(handles)
table_values = string([ wd Wp Talwd Malwd alphaalwd]);
table_param = [table_titles; [table_values]];
endfunction
Now you can call this function to get the variable:
string(tab_param_callback(handles))
I corrected just this case and added the dots as explained. For the other variables, it can be done analogously. Furthermore, I moved the function definition to the beginning of the code. Because if your scrips crashed in the middle, the compiler has no chance to read the function definitions.
Please find the code in this file .
I hope this helps. Good luck!

Updating multiple variables in case statement

I've been looking around for a while so forgive me if maybe I'm using improper terminology...
The goal of the code is to update Aout1 and Aout0 if the input is 0, with the output corresponding to a 7-segment display, but I'm getting the following error:
"Error (10170): Verilog HDL syntax error at FourBitAdder.v(55) near text: ","; expecting ";". Check for and fix any syntax errors that appear immediately before or at the specified keyword."
Below is a snippet of the code giving me issues...
always #*
case (A)
4'b0000 : Aout1 = 7'b1000000, Aout0 = 7'b1000000; //00
I tried changing the code to the following and while I didn't get any errors on my software, my hardware (7-segment display) isn't working like it was when I was trying to just change one variable per case.
always #*
case (A)
4'b0000 : Aout1 = 7'b1000000; 4'b0000 : Aout0 = 7'b1000000; //00
Thank you in advance :)
Use a begin and end statement after the colon.
always #* begin
case(A)
4'b0000: begin
Aout1 = 7'b1000000;
Aout0 = 7'b1000000;
end
4'b0001: begin
Aout1 = 7'b0000011;
Aout0 = 7'b0000011;
end
endcase
end

A datetime arithmetic in MQL4

I would like to define a datetime type variable that is a result of a simple arithmetic operation between datetime type variables.
I've defined:
datetime duration = ( TimeCurrent() - OrderOpenTime() );
datetime TmStop = StringToTime( "1970.01.01 16:00" );
but when I call it in some other arithmetic operation or generally in code like this
ExitBuy_H1 = ( duration > TmClose && ...
or this
text[3]= "Duration: " + TimeToStr( duration, TIME_MINUTES );
it doesn't work.
TmStop instead works fine.
Does anyone know why?
datetime is a simple integer, number of seconds passed since 1970.01.01 00:00. duration in your example is also in seconds, even though it is datetime formated, when you need it in minutes, divide by 60. TmClose from your example means 16*60*60 seconds and you can compare that integer with any other int of course, but what might be the reason for that?
if you hold you position more then 16 hours, then duration > TmClose is true. if you want to convert difference in seconds (duration) into time, then you will have time converted from 1970.01.01 00:00 + duration seconds.
Anyway it is not clear what is your goal in doing this calculations? if you want to make sure that you hold that particular position more then x hours, then simple bool holdMoreThanXHours = TimeCurrent()-OrderOpenTime()>x*PeriodSeconds(PERIOD_H1), and do not forget to reselect each ticket if you have several ones in open
Fact A) the code, as-is, absolutely out of any question works.
//+------------------------------------------------------------------+
//| Test_StackOverflow.mq4 |
//+------------------------------------------------------------------+
#property strict
void OnStart() {
datetime duration = ( TimeCurrent() - OrderOpenTime() );
string txt = "Duration: " + TimeToStr( duration, TIME_MINUTES );
}
//+------------------------------------------------------------------+
0 error(s), 0 warning(s), compile time: 2000 msec 1 1
Fact B) the full MCVE-context of the code, as-is, is missing.
StackOverflow requires users to post a complete MCVE-representation of the problem. This requirement was not met in the original post.
While the datetime and int data-types are mutually interchangeable, the problem does not seem to be hidden in this intrinsic "duality" of a value representation, but must be somewhere else.
The main suspects for Why? are:
variable definition was masked by another variable having the same name
variable scope-of-definition was exceeded ( asking a variable outside of it's scope )
db.Pool-operations were not preceded by OrderSelect()

initialization of variable in pre in Modelica

I wrote the codes in Modelica as below:
model TestIniitial
extends Modelica.Icons.Example;
parameter Integer nWri= 2;
Real u[nWri](each start= 10, fixed=false);
Real uPre[nWri];
parameter Real _uStart[nWri] = fill(10, nWri);
parameter Modelica.SIunits.Time startTime = 0;
parameter Modelica.SIunits.Time samplePeriod = 1;
Boolean sampleTrigger "True, if sample time instant";
initial equation
u[1] = 1;
u[2] = 2;
equation
sampleTrigger = sample(startTime, samplePeriod);
when sampleTrigger then
for i in 1: nWri loop
uPre[i] = pre(u[i]);
end for;
end when;
for i in 1:nWri loop
u[i] = (i+1)*time;
end for;
end TestIniitial;
Basically I want to initialize the u before simulation. However, I got below complaints(the initialization of u is over-specified) from translation:
The Modelica Language Specification 3.2.1 specifies that if a real variable, v,
is appearing in an expression as pre(v), but not assigned by a when equation,
then the equation v = pre(v) should be added to the initialization problem.
For this problem the following equations were added:
u[1] = pre(u[1]);
u[2] = pre(u[2]);
I can't understand the complaints since pre(v) was assigned in when equation already. What can I do if I want to initialize the u in above codes?
Thanks.
Looking at this, my guess is that the error message is trying to provide you some diagnostics but it is incorrect about the source. I suspect (again, I do not know for sure) that it sees the fact that pre(u) appears in the model and that there is an initialization problem and assumes a specific issue.
My guess is that the issue stems from the fact that you have fixed=true set on u. I see no reason to do that and my guess is that it will lead to too many constraints on the initialization problem as well. Get rid of the fixed=true and see what happens. Report back if that doesn't address the problem.
Good luck.

systemverilog constraint dist using weights array

I need to be able to set a constraint dist with 64 different, changeble weights:
I need to random pick an index of range 0~63, when every index has its own weight / probability to be chosen.
I can write something like:
constraint pick_chan_constraint {pick_channel dist{
0:=channel_weight[0], 1:=channel_weight[1], 2:=channel_weight[2],
3:=channel_weight[3], 4:=channel_weight[4], 5:=channel_weight[5],
6:=channel_weight[6], 7:=channel_weight[7], 8:=channel_weight[8],
9:=channel_weight[9], 10:=channel_weight[10], 11:=channel_weight[11],
12:=channel_weight[12], 13:=channel_weight[13],
14:=channel_weight[14], … ...
NUM_OF_CHANS-1 := channel_weight[NUM_OF_CHANS-1] }}
Obviously it's bad writing and a bad idea, out of 2 reasons:
No flexibility- if NUM_OF_CHANS changes, I'll need to change the code.
It's long and ugly and almost unreadable.
Any ideas?
Thanks
IEEE Std 1800-2012 § 18.5.4 Distribution shows the dist_list needs to be a list of dist_items and a dist_item is defined as a value_range [ dist_weight ]. In other words the distribution needs to be listed out.
Instead of using a constraint you could create a queue array (§ 7.10 Queues) and then use the shuffle method (§ 7.12.2 Array ordering methods). Example:
int channel_weight [64];
int pick_channel;
int weight_chain [$];
weight_chain.delete(); // make sure it is empty
foreach (channel_weight[i]) begin
repeat (channel_weight[i]) begin
weight_chain.push_back(i);
end
end
weight_chain.shuffle(); // randomize order
assert( weight_chain.size() > 0) else $error("all channel_weights are 0");
pick_channel = weight_chain[0];

Resources