vhdl - Compile Date and Time in FPGA -
can uses in vhdl similar c-sourcecode-macros __date__
, __time__
make compile time available in fpga kind of version time stamp?
as >>>new-comer<<< vhdl want modify following existing code, puts hard coded date fpga register. have remember adjusting values before compiling. easier if done automatically. can include hours/minutes/seconds?
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; entity datum2 port ( day :out std_logic_vector(4 downto 0); month :out std_logic_vector(3 downto 0); year :out std_logic_vector(4 downto 0) ); end datum2 ; architecture rtl of datum2 begin -- "08.08.0013" day <= conv_std_logic_vector(8, 5); month <= conv_std_logic_vector(8, 4); year <= conv_std_logic_vector(13, 5); end architecture rtl;
current date , time not directly available in vhdl, solution suggested below.
edited 2013-08-10: added description altera quartus ii tcl automatic generation.
one way make date , time available through automatically generated vhdl package below:
library ieee; use ieee.std_logic_1164.all; package datetime -- date information constant year_int : integer := 2013; constant year_hex : std_logic_vector(15 downto 0) := x"2013"; constant month_int : integer := 08; constant month_hex : std_logic_vector(7 downto 0) := x"08"; constant day_int : integer := 09; constant day_hex : std_logic_vector(7 downto 0) := x"09"; constant date_hex : std_logic_vector(31 downto 0) := year_hex & month_hex & day_hex; -- time information constant hour_int : integer := 13; constant hour_hex : std_logic_vector(7 downto 0) := x"13"; constant minute_int : integer := 06; constant minute_hex : std_logic_vector(7 downto 0) := x"06"; constant second_int : integer := 29; constant second_hex : std_logic_vector(7 downto 0) := x"29"; constant time_hex : std_logic_vector(31 downto 0) := x"00" & hour_hex & minute_hex & second_hex; -- miscellaneous information constant epoch_int : integer := 1376046389; -- seconds since 1970-01-01_00:00:00 end package;
this vhdl package can created tcl script this:
# make datetime.vhd package tcl script # current date, time, , seconds since epoch # array index 0 1 2 3 4 5 6 set datetime_arr [clock format [clock seconds] -format {%y %m %d %h %m %s %s}] # write vhdl package set filename datetime.vhd set file [open $filename w] puts $file "library ieee;" puts $file "use ieee.std_logic_1164.all;" puts $file "" puts $file "package datetime is" puts $file " -- date information" puts $file " constant year_int : integer := [lindex $datetime_arr 0];" puts $file " constant year_hex : std_logic_vector(15 downto 0) := x\"[lindex $datetime_arr 0]\";" puts $file " constant month_int : integer := [lindex $datetime_arr 1];" puts $file " constant month_hex : std_logic_vector(7 downto 0) := x\"[lindex $datetime_arr 1]\";" puts $file " constant day_int : integer := [lindex $datetime_arr 2];" puts $file " constant day_hex : std_logic_vector(7 downto 0) := x\"[lindex $datetime_arr 2]\";" puts $file " constant date_hex : std_logic_vector(31 downto 0) := year_hex & month_hex & day_hex;" puts $file " -- time information" puts $file " constant hour_int : integer := [lindex $datetime_arr 3];" puts $file " constant hour_hex : std_logic_vector(7 downto 0) := x\"[lindex $datetime_arr 3]\";" puts $file " constant minute_int : integer := [lindex $datetime_arr 4];" puts $file " constant minute_hex : std_logic_vector(7 downto 0) := x\"[lindex $datetime_arr 4]\";" puts $file " constant second_int : integer := [lindex $datetime_arr 5];" puts $file " constant second_hex : std_logic_vector(7 downto 0) := x\"[lindex $datetime_arr 5]\";" puts $file " constant time_hex : std_logic_vector(31 downto 0) := x\"00\" & hour_hex & minute_hex & second_hex;" puts $file " -- miscellaneous information" puts $file " constant epoch_int : integer := [lindex $datetime_arr 6]; -- seconds since 1970-01-01_00:00:00" puts $file "end package;" close $file
in altera quartus ii possible make flow run script before synthesis, whereby datetime package can created. done in .qsf file line below, script named "make_datetime.tcl":
set_global_assignment -name pre_flow_script_file "quartus_sh:make_datetime.tcl"
further description of quartus ii feature can found in quartus ii tcl example: automatic script execution.
the datum2 module can use package this:
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; entity datum2 port( day : out std_logic_vector(4 downto 0); month : out std_logic_vector(3 downto 0); year : out std_logic_vector(4 downto 0)); end datum2; library work; use work.datetime; architecture rtl of datum2 begin day <= conv_std_logic_vector(datetime.day_int, 5); month <= conv_std_logic_vector(datetime.month_int, 4); year <= conv_std_logic_vector(datetime.year_int mod 100, 5); end architecture rtl;
after synthesis in quartus ii rtl viewer shows module outputs below:
previously described alternative solutions create vhdl package bash script this:
# make datetime.vhd package shell script # current date, time, , seconds since epoch # array index 0 1 2 3 4 5 6 datetime_arr=($(date +"%y %m %d %h %m %s %s")) # write vhdl package filename="datetime.vhd" echo "library ieee;" > $filename echo "use ieee.std_logic_1164.all;" >> $filename echo "" >> $filename echo "package datetime is" >> $filename echo " -- date information" >> $filename echo " constant year_int : integer := ${datetime_arr[0]};" >> $filename echo " constant year_hex : std_logic_vector(15 downto 0) := x\"${datetime_arr[0]}\";" >> $filename echo " constant month_int : integer := ${datetime_arr[1]};" >> $filename echo " constant month_hex : std_logic_vector(7 downto 0) := x\"${datetime_arr[1]}\";" >> $filename echo " constant day_int : integer := ${datetime_arr[2]};" >> $filename echo " constant day_hex : std_logic_vector(7 downto 0) := x\"${datetime_arr[2]}\";" >> $filename echo " constant date_hex : std_logic_vector(31 downto 0) := year_hex & month_hex & day_hex;" >> $filename echo " -- time information" >> $filename echo " constant hour_int : integer := ${datetime_arr[3]};" >> $filename echo " constant hour_hex : std_logic_vector(7 downto 0) := x\"${datetime_arr[3]}\";" >> $filename echo " constant minute_int : integer := ${datetime_arr[4]};" >> $filename echo " constant minute_hex : std_logic_vector(7 downto 0) := x\"${datetime_arr[4]}\";" >> $filename echo " constant second_int : integer := ${datetime_arr[5]};" >> $filename echo " constant second_hex : std_logic_vector(7 downto 0) := x\"${datetime_arr[5]}\";" >> $filename echo " constant time_hex : std_logic_vector(31 downto 0) := x\"00\" & hour_hex & minute_hex & second_hex;" >> $filename echo " -- miscellaneous information" >> $filename echo " constant epoch_int : integer := ${datetime_arr[6]}; -- seconds since 1970-01-01_00:00:00" >> $filename echo "end package;" >> $filename
for platform independence python 3.x script may used:
# make datetime.vhd package python 3.x script # date , time import datetime import time = datetime.datetime.now() now_epoch_sec = int(time.time()) # write vhdl package file = open('datetime.vhd', 'wt') file.write('library ieee;\n') file.write('use ieee.std_logic_1164.all;\n') file.write('\n') file.write('package datetime is\n') file.write(' -- date information\n') file.write(' constant year_int : integer := {};\n'.format(now.strftime('%y'))) file.write(' constant year_hex : std_logic_vector(15 downto 0) := x\"{}\";\n'.format(now.strftime('%y'))) file.write(' constant month_int : integer := {};\n'.format(now.strftime('%m'))) file.write(' constant month_hex : std_logic_vector(7 downto 0) := x\"{}\";\n'.format(now.strftime('%m'))) file.write(' constant day_int : integer := {};\n'.format(now.strftime('%d'))) file.write(' constant day_hex : std_logic_vector(7 downto 0) := x\"{}\";\n'.format(now.strftime('%d'))) file.write(' constant date_hex : std_logic_vector(31 downto 0) := year_hex & month_hex & day_hex;\n') file.write(' -- time information\n') file.write(' constant hour_int : integer := {};\n'.format(now.strftime('%h'))) file.write(' constant hour_hex : std_logic_vector(7 downto 0) := x\"{}\";\n'.format(now.strftime('%h'))) file.write(' constant minute_int : integer := {};\n'.format(now.strftime('%m'))) file.write(' constant minute_hex : std_logic_vector(7 downto 0) := x\"{}\";\n'.format(now.strftime('%m'))) file.write(' constant second_int : integer := {};\n'.format(now.strftime('%s'))) file.write(' constant second_hex : std_logic_vector(7 downto 0) := x\"{}\";\n'.format(now.strftime('%s'))) file.write(' constant time_hex : std_logic_vector(31 downto 0) := x\"00\" & hour_hex & minute_hex & second_hex;\n') file.write(' -- miscellaneous information\n') file.write(' constant epoch_int : integer := {}; -- seconds since 1970-01-01_00:00:00\n'.format(now_epoch_sec)) file.write('end package;\n') file.close()
for presentation of date , time in 32-bit register values, module can this:
library ieee; use ieee.std_logic_1164.all; entity tb end entity; library work; use work.datetime; architecture sim of tb signal date_hex : std_logic_vector(31 downto 0); signal time_hex : std_logic_vector(31 downto 0); begin date_hex <= datetime.date_hex; time_hex <= datetime.time_hex; process begin wait; end process; end architecture;
waveform shown below.
the bash or python script approach requires integration in build flow automatic package generation.
edited 2016-08-08 update damien: description of non-tcl script call altera quartus.
to integrate bash script (under linux), create tcl wrapper script calls bash script part of process. in example, there "scripts" directory has "call_bash.tcl" script , "make_datetime.sh" actual work:
# useful if script in subdirectory proc getscriptdirectory {} { set dispscriptfile [file normalize [info script]] set scriptfolder [file dirname $dispscriptfile] return $scriptfolder } set scriptdir [getscriptdirectory] # call bash script real work exec $scriptdir/make_datetime_vhdl.sh # add message altera workflow post_message -type info "created datetime.vhd"
integrating altera's build flow can achieved adding following the qsf file:
set_global_assignment -name pre_flow_script_file "quartus_sh:scripts/call_bash.tcl"
Comments
Post a Comment