Welcome EEWeb Users

In case you’ve stumbled across my blog from EEWeb, I just want to thank you for coming to check out my little corner of the internet. Feel free learn a little About me, peruse the articles I have written and perhaps, if you’re intrigued, you’ll subscribe to my RSS feed and stop by now and then as new posts go up.

I’ll warn you that my idle time has been heavily consumed by some courses I’ve been taking through UC Berkeley Extension. I’ve been working my way through their Integrated Circuit Design and Techniques Certificate program. Therefore, posting frequency has been lighter lately than I would like. All the more reason to subscribe to the RSS feed ;-)

I’ve put together a little list of my more popular posts here on Idle-Logic.com:



Talking to the DE0-Nano using the Virtual JTAG interface.

Shows a technique to setup a communication path between the PC and DE0-Nano using the existing USB connection. With this TCP/IP bridge you can communicate with the device using practically any programming language.

DE0-Nano to Python via TCL



Configuring the Cyclone II FPGA

The title about says it all. For this post, I was using a basic FPGA Breakout board. If you dig a little deeper you can see many posts talking about the how the Cyclone II works, what clock I used, and how I powered the device.



Programming an Altera Cyclone II FPGA with a FT232RL USB to UART Bridge

Here I show how to program the FPGA via USB instead of using the Parallel port. I found a way to manually bit bang the configuration file into the FPGA using Python and an FT232RL breakout board.


Using ModelSim with Quartus II and the DE0-Nano

This is a little crash course on how to use ModelSim with Quartus design files. I’m planning a new blog post here in the near future talking a little more about fun ModelSim features, and a little more detail about how to exercise the waveforms.


TCL Macro for Top-Level Schematic to Verilog Conversion (For ModelSim Simulation)

For people who like a schematic top-level design, this post shows code I developed to automatically convert the Quaratus top-level schematic to HDL for simulation. (Since ModelSim doesn’t accept schematic files).



Hope you enjoy, thanks again for dropping by.

The Curious Case of the Sho BindingFailure

Seeing strange inexplicable issues in your Visual Basic .NET code? Keep this option in your back pocket… try to delete the Project’s .suo file.

I ran into the following error while trying to integrate Microsoft’s Sho library into a VB.NET project at work:

For the sake of google indexing, the error is:

BindingFailure was detected
The assembly with display name ‘ArrayT’ failed to load in the ‘Load’ binding context of the AppDomain with ID1. The cause of the failure was: System.IO.FileNotFoundException

I made sure to closely follow the installation information, and searching through the directory structure I found the ArrayT.dll was indeed in the /bin/ directory. It actually had two versions one for x86 another for x64. I tried to move each one at a time into the base bin directory just to try and help .NET track it down, no luck. I tried blowing away the compiled objects in the Projects own /bin/ and /obj/ directories, no luck. I tried using the Visual Studio menu option to Clean Solution, this manages to horribly break any ActiveX controls you might have added to a Form, and didn’t fix the Sho problem. (In another post I’ll talk about how to fix this ActiveX problem).

I downloaded the Sho C# example program, which had no problems running. Very curious.

Next, I tried creating a new VB project from scratch and added Sho… no problems. Perhaps its a pathing issue I thought, so I moved the new same named VB program into the original’s directory, the new project still worked.

At this point, I decided the problem might be related to the fact the non-working VB project was originally from an older version of Visual Studio and pushed to VS2010 using the auto-upgrade tools. I noticed there were significant differences between the two project’s core files, the .vbproj, .sln, .vbproj.user and several of the files in the /My Project/ subdirectory.

My next move was to recreate my original broken project from scratch in a shiny brand new VB2010 project. (A herculean task for a project this size). After the project was rebuilt, I added in Sho, and clicked run… and magically the BindingFailure was no more! Very curious.

Now in a quest to understand why the difference in behavior between the two projects, I spent the next several hours, with the help of Kdiff3, merging all of differences from the new project into the old project. Boy was there a lot of junk added into the old project from the VS2005 to VS2008 to VS2010 Upgrade path. After every few lines of merging code, I would rerun the solution to see if the problem was fixed. After all the files were almost identical (things like GUID‘s needed to be preserved), the problem was still not fixed.

My last resort was to just start deleting non user created code. My first choice, for reasons I’ll explain in a moment, was to blow away the project’s .suo file, a file which seems to be mostly unreadable binary format. After removing this file (actually I just renamed it) I fired up the project only to find the Error was gone!, my problem was solved just by deleting, and letting Visual Studio rebuild this file. Why? I have absolutely no idea. I sent an email to the Sho team divulging the same details of this adventure and I have yet to hear their thoughts on it. I’m staying hopeful some gears are grinding inside Microsoft to get this issue fixed.

The project I’ve been working on not only has been through a long upgrade path, but has also been switched between x64 and x86, and passed between developers. Unfortunately, I pulled the project from source control before this .suo file was excluded from the commit. Had it been excluded from the initial commit, I might never have seen this issue. However, this is also how I was able to directly target the .suo when I went into “start deleting arbitrary files” mode.

Basically, I opened the .suo file in Kdiff3 and noticed some fragmented bits of file paths. I noticed that some of my colleagues names were in the file paths, stuff like: C:\Users\{Colleague's Name}\... , somehow it seems this persisted information was enough to totally and un-obviously break Sho’s driver bindings, or perhaps its path resolution algorithm.

So lesson learned, when all else fails… actually, long before all else fails, delete that .suo project file.

Fingers crossed this blog post saves someone else from a day full of frustration.

If this has helped you, or you’ve had similar head scratching issues like this, leave a comment below and we can take solace in commiserating.

Issue after Quartus v12 SP2 Upgrade

This week I upgraded some work projects to Quartus v12 SP2 and I ran into an interesting bug, which has an obscure patch to fix it. I thought I should share.

The problems happens when you update a symbol/block in a schematic (bdf) file, after which anytime you try to compile you get a nasty error that crashes Quartus.

The error code is basically this:
Internal Error: Sub-system: DBMUI, File: /quartus/db/dbmui/dbmui_manager.cpp, Line: 1179

At this point I found you need to apply this Patch 2.16 to fix the problem.

When I ran the patch installer, it was unable to locate my Quartus installation. I first tried to use the quartus subfolder, but eventually found I needed to patch to this directory:
C:\altera\12.0sp2

Hopefully that helps solve your problem.

Mercurial Source Control for Quartus Projects

In order to better manage my development of Quartus HDL projects I decided to start using source control software. This allows me to easily see the changes I’ve made through the use of diff tools and actually forces me to document the changes I’m making, while giving me the ability to rollback those changes at any time.

A lot of the source control tools are designed for large traditional software development teams which include a lot of tools for branching and merging. I wanted a widely accepted tool, but one without too much of a learning curve and simple enough to work for the development team consisting of just me.

The big names in source control right now seem to be: Subversion, Git, and Mercurial. Some GUI tools are also available to help you from having to wrestle with the command line: TortoiseSVN, Git GUIs, and TortoiseHg .

After trying all three I found Mercurial with TortoiseHg was the best fit for my workflow.

Here is a nice Quick start Guide to TortoiseHg: http://tortoisehg.bitbucket.org/manual/1.1/quick.html

Even though I’m not doing much collaborative development right now, I think it’s good to understand the different collaboration models available in Mercurial, this link does a good job explaining them: http://hgbook.red-bean.com/read/collaborating-with-other-people.html


.hgignore File

One of the important bits needed to get Mercurial/TortoiseHg working with Quartus projects is correctly setting up the hgignore file. This is the file that tells Mercurial what files not to track. Typically, you only want source control tools to track human created code.

As of right now this is the hgignore file I’ve come up with, which I thought might be useful to share. (I haven’t done a ton of rolling back to test this yet, so your mileage may vary)

# use glob syntax
syntax: glob

*.flock
*.rpt
*.done
*.summary
*.smsg
*.qmsg
*.pof
*.sof
*.rbf
*.jdi
*.cdb
*.hdb
*.rdb
*.ddb
db/**
incremental_db/**


Feel free to leave a comment and let me know if this is useful to you, or if you have any recommended changes to the hgignore template above.

TCL Macro for Top-Level Schematic to Verilog Conversion (For ModelSim Simulation)

Quick Summary:

I created a handy script (b2vFixer.tcl) that prepares a Quartus project with a Schematic (BDF) top-level entity for use in ModelSim.

My personal preference is to use a Schematic for the Top-Level entity of my FPGA projects. I find that by drawing out the top level it becomes self documenting and easier to understand the general interconnections of the project. One drawback of this approach is that ModelSim doesn’t support Schematic files. As I pointed out in a previous post you have to use a Quartus tool to convert the Schematic to HDL, add this file to the project, remove the schematic file from the project, and finally set the new HDL file as the top-level.

After all that work you find that Quartus only half halfheartedly converted your schematic file. You can see that instead of naming the module instances something useful, it defaults to b2v_inst#:


Which becomes less than useful in ModelSim:


To alleviate this problem, I put together a TCL script that automates the conversion, it reprocesses the converted Verilog file (improves the instance names) and updates the Top-Level entity for you.

Here you can see the improvements after you use the b2vFixer.tcl script:


Making things more clear in ModelSim:




How to use b2vFixer.tcl


1. The first step is to download and save the b2vFixer.tcl script to your project’s directory.

2. The next step is to open your project in Quartus and open the TCL Console:

3. Load the b2vFixer.tcl by typing source b2vFixer.tcl in the console:

3b. Alternatively, you can add source b2vFixer.tcl to your project’s .qsf file to have it automatically loaded for you every time you open the project:


4. Kick off the conversion process by calling the b2v function in the console:

5. You should now have the automatically generated Verilog file added to your project and the top-level entity updated. The b2vFixer changes the top-level module name by adding a _b2v suffix to the end, which allows you to keep both the Verilog and BDF file in your project. (This might slow down the compilation process. Feel free to uncomment line 180 in the source code and the BDF file will be auto removed.)

Reverting back to the Schematic

After you’re done with the simulation, it’s a breeze to return the Schematic as the top level, you just need to right click the bdf file and Set as Top-Level Entity:


Feel free to leave the auto-generated Verilog _b2v file in your project. It will be automatically overwritten next time you run the b2v function.


Known b2vFixer.tcl Limitations

1. Quartus doesn’t do the best job of keeping track of what type of file the top-level entity is (bdf or HDL) which makes it tricky for my script to know if it’s a schematic or not. Currently if the top-level ends in _b2v, then b2vFixer.tcl knows not to try and do a conversion. A fix I found for this is to always run the Quartus Heirarchy Elaboration, then the top level type can be detected correctly. The following code can do this:

execute_module -tool map -args -elaborate_hierarchy
set current_focus_entity_id [get_top_level_entity] 
puts [get_name_info -info file_location $current_focus_entity_id]

(If you added “source b2vFixer.tcl” to the qsf file like I described above, for some reason, the b2v function is being called when you use the elaborate_hierarchy tcl command. I need to look into this a little closer before the functionality can be added.)

2. You’ll probably have problems in ModelSim if you project’s top-level contains other Schematic files, b2vFixer.tcl doesn’t do any form of recursive conversion.

3. Currently b2v only works with the top-level schematic, we could probably allow it to accept an argument and convert any bdf file pretty easily.

4. Doesn’t support VHDL yet.



Download b2vFixer.tcl

Download the script here:
Download
b2vFixer.zip

Or just copy/paste the source code:

#File:           	b2vFixer.tcl
#Author:         	Chris Zeh <Chris@idle-logic.com> 
#Website:			idle-logic.com
#Version:        	0.9b
#License			MIT License (http://www.opensource.org/licenses/mit-license.php)
# ----------------------------------------------
#		Revision History
# ----------------------------------------------
#	Version		Date		Comments
# ----------------------------------------------
#	0.9b		06/07/12	Initial Release
# ----------------------------------------------
#
#Purpose:        	
#	In order to prepare a project for ModelSim-ASE (Which doesn't allow
#	schematic top-level files, this tool converts and improves the Top-Level
#	Schematic (bdf) to Verilog conversion and prepares the project for
#	ModelSim.
#	Note: VHDL conversion not yet supported.
#
#Usage:
#	From the Quartus TCL Console:
#	tcl> source b2vFixer.tcl
#	tcl> b2v
#
#	You can also add "source b2vFixer.tcl"	to the project's .qsf file for it
#	to be included automatically to the project

#Global declarations:
set lModNames {}
set lModCount {}

# --------------------------------------------------------------------------------------------
#Function:	get_inst_number
#Purpose:	Keeps track of the Modules detected during the file processing and 
#			returns the instance number. Allowing us to manage something like
#			clk_inst0 and clk_inst1 in the same project
#
#Inputs:	args[0] is the String name of the module
#			Uses the global static variables, lModNames & lModCount
#Returns:	Returns an integer with the instance number
# --------------------------------------------------------------------------------------------
proc get_inst_number args {
	#This might be a little cleaner using TCL Arrays instead of Lists.
	global lModNames
	global lModCount 
	
	set NewModName [lindex $args 0]
	
	#Check to see if the NewModule is already in our List of Modules
	set ModIndex [lsearch $lModNames $NewModName]
	if {$ModIndex == -1} {
		#Module is new
		#Append it to the list here
		lappend lModNames $NewModName
		lappend lModCount 0
		return 0
	} else {
		#Module has already been found previously
		#Increment the list count
		lset lModCount $ModIndex [expr {[lindex $lModCount $ModIndex]+1}]
		return [lindex $lModCount $ModIndex]
	}
	
	}
	
# --------------------------------------------------------------------------------------------
#Function:	start_quartus_b2v
#Purpose:	Starts the native Quartus bdf to verilog tool. Checks to see if the top level has
#			already been converted by this tool, indicated by a _b2v suffix. The top-level 
#			file checking isn't robust yet since in Quartus there doesn't appear to be an easy
#			way to detect its file type.
#
#Inputs:	None. 
#			Resets the global static variables, lModNames & lModCount
#Returns:	Returns boolean success
# --------------------------------------------------------------------------------------------
proc start_quartus_b2v args {
	if {![is_project_open]} { 
		puts "No Project Open"
		return False
	}
	#Reset the Module List/Counter
	global lModNames
	global lModCount 
	set lModNames {}
	set lModCount {}
	
	#Check to see what the current toplevel is	
	set toplevel_name [get_global_assignment -name TOP_LEVEL_ENTITY]
	puts "Top-Level Detected: $toplevel_name"
		
	#See if it is already a "toplevel_b2v" Verilog File
	set suffix_test [string range $toplevel_name end-3 end]
	if {$suffix_test == "_b2v"} {
		puts "Top Level is already a converted schematic file"
		return False
	}
	
	#Kick off the Quartus b2v tool
	set toplevel_bdf "${toplevel_name}.bdf"
	set convert_arg "--convert_bdf_to_verilog=$toplevel_bdf"
	puts "Starting Quartus native b2v tool..."
	if {[catch {execute_module -tool map -args $convert_arg} result]} {
		puts "ERROR: Issue while converting. Check for Errors in the Messages Window.\n"
		return False
	} else {
		puts "...Completed successfully \n"
		
	}
	return True
}

# --------------------------------------------------------------------------------------------
#Function:	b2v
#Purpose:	Main Function call for this program. Initiates and manages the full b2v
#			and processing. 
#
#Inputs:	None. 			
#Returns:	Returns boolean success
# --------------------------------------------------------------------------------------------
proc b2v args {
	if {![is_project_open]} { 
		puts "No Project Open"
		return
	}
	#Start the Internal Quartus Schematic (bdf) to Verilog Converter
	puts "Converting Schematic Top-Level Entity to Verilog Top-Level"
	puts "*******************************************************************"

	if { ![start_quartus_b2v]} {
		return
	}
	
	set toplevel_name [get_global_assignment -name TOP_LEVEL_ENTITY]
	
	set module_name "module ${toplevel_name}("
	set new_module_name "module ${toplevel_name}_b2v("
	set b2vFile [open "${toplevel_name}_b2v.v" w]
	set File [open [file join [pwd] "${toplevel_name}.v"]]
	
	#We will traverse the Quartus Converted b2v file, and make our adjustments.
	#Swap the default b2v_inst0 names for an inst based on the module name so vJTAG_inst0
	#	Also keep track of the module names so we can deal with duplicates, so: vJTAG_inst1, vJTAG_inst2, and so on.
	#Also, we want to swap the module name to <toplevel>_b2v, which will allow the schematic file and the converted
	#	verilog file to remain in the project. Otherwise, you get an error w/ duplicate modules at compile time.
	#	This might slow down the compile process, so if we want to pull the bdf out of the project we can use
	#	set_global_assignment -name BDF_FILE ${toplevel_name}.bdf -remove
	
	puts "Starting Quartus b2v File Processing"
	puts "-------------------------------"
	puts "Top Level Modules Found:"
	puts "-------------------------------"

	foreach {i} [split [read $File] \n] {
		set mod_name [lindex [regexp -inline -all -- {\S+} $i] 0]
		set inst_name [lindex [regexp -inline -all -- {\S+} $i] 1]
		set inst_name_trunc [string range $inst_name 0 7]

		if {$inst_name_trunc == "b2v_inst"} then {
			set inst_num [get_inst_number $mod_name]
			puts $b2vFile "${mod_name}   ${mod_name}_inst${inst_num}("
			puts "${mod_name}_inst${inst_num}"
		} elseif {$i == $module_name} then {
			
			puts $b2vFile $new_module_name
			
		} else {
			#puts $i
			puts $b2vFile $i
		}

	}
	puts "-------------------------------"
	close $File
	close $b2vFile
	
	set_global_assignment -name VERILOG_FILE ${toplevel_name}_b2v.v
	set_global_assignment -name TOP_LEVEL_ENTITY ${toplevel_name}_b2v
	#set_global_assignment -name BDF_FILE ${toplevel_name}.bdf -remove
	
	puts "Schematic to Verilog Top-Level Conversion Complete:"
	puts "	${toplevel_name}_b2v.v added to the project"
	puts "	${toplevel_name}_b2v set as TOP_LEVEL_ENTITY"
}

#Indicate the file has been loaded:
puts "------------------------------------"
puts "b2vFixer.tcl Loaded:"
puts "b2v Command now available"
puts "------------------------------------"




# --------------------------------------------------------------------------------------------
# Copyright (c) 2012 Chris Zeh
#
# Released under the MIT License (MIT) (http://www.opensource.org/licenses/mit-license.php)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, 
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or 
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# --------------------------------------------------------------------------------------------