How to create a n-bit counter using GAL, programming in WinCUPL - hardware

No, I need the shortest possible solution, preferably with macro folding.

Here is how I did this, cascading Repeat macros:
Name 8-bit counter ;
PartNo MIC20181 ;
Date 2015/2/8 ;
Revision 01 ;
Designer Engineer ;
Company Donghua University ;
Assembly None ;
Location ;
Device g16v8 ;
/* Input pins */
Pin 1 = CLK;
Pin 11 = GND;
/* Output pins */
PinNode [12..19] = [Q7..0];
Append Q0.D = !Q0;
Append Q1.D = !Q1 & Q0;
Append Q1.D = Q0 & !Q1;
$Repeat P = [2..7]
$Repeat R = [{P - 1}..0]
Append Q{P}.D = Q{P} & !Q{R};
$RepEnd
Append Q{P}.D = !Q{P} & !([Q{P-1}..0]:#);
$RepEnd
The trick here is how I (ab)used $Repeat macros.

Related

Using levenshtein on parts of string in SQL

I am trying to figure out a way to work some fuzzy searching methods into our store front search field using the Levenshtein method, but I'm running into a problem with how to search for only part of product names.
For example, a customer searches for scisors, but we have a product called electric scissor. Using the Levenshtein method levenshtein("scisors","electric scissor") we will get a result of 11, because the electric part will be counted as a difference.
What I am looking for is a way for it to look at substrings of the product name, so it would compare it to levenshtein("scisors","electric") and then also levenshtein("scisors","scissor") to see that we can get a result of only 2 in that second substring, and thus show that product as part of their search result.
Non-working example to give you an idea of what I'm after:
SELECT * FROM products p WHERE levenshtein("scisors", p.name) < 5
Question: Is there a way to write an SQL statement that handles checking for parts of the string? Would I need to create more functions in my database to be able to handle it perhaps or modify my existing function, and if so, what would it look like?
I am currently using this implementation of the levenshtein method:
//levenshtein(s1 as VARCHAR(255), s2 as VARCHAR(255))
//returns int
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END
This is a bit long for a comment.
First, I would suggest using a full-text search with a synonyms list. That said, you might have users with really bad spelling abilities, so the synonyms list might be difficult to maintain.
If you use Levenshtein distance, then I suggest doing it on a per word basis. For each word in the user's input, calculate the closest word in the name field. Then add these together to get the best match.
In your example, you would have these comparisons:
levenshtein('scisors', 'electric')
levenshtein('scisors', 'scissor')
The minimum would be the second. If the user types multiple words, such as 'electrk scisors', then you would be doing
levenshtein('electrk', 'electric') <-- minimum
levenshtein('electrk', 'scissor')
levenshtein('scisors', 'electric')
levenshtein('scisors', 'scissor') <-- minimum
This is likely to be an intuitive way to approach the search.

Build formal model of UART in NuSMV?

I'm learning model-checking and NuSMV for my education. I can edit and run NuSMV code and I have a fair understanding of what UART is and does.
My task is to formally model UART with NuSMV but at this time I'm not sure how to do it. I understand that UART transmits one byte as eight sequential bits but how can I model that?
I have a mutex code as a starting point:
>NuSMV.exe mutex.smv
*** This is NuSMV 2.6.0 (compiled on Wed Oct 14 15:37:51 2015)
*** Enabled addons are: compass
*** For more information on NuSMV see <http://nusmv.fbk.eu>
*** or email to <nusmv-users#list.fbk.eu>.
*** Please report bugs to <Please report bugs to <nusmv-users#fbk.eu>>
*** Copyright (c) 2010-2014, Fondazione Bruno Kessler
*** This version of NuSMV is linked to the CUDD library version 2.4.1
*** Copyright (c) 1995-2004, Regents of the University of Colorado
*** This version of NuSMV is linked to the MiniSat SAT solver.
*** See http://minisat.se/MiniSat.html
*** Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
*** Copyright (c) 2007-2010, Niklas Sorensson
-- specification EF (state1 = c1 & state2 = c2) is false
-- as demonstrated by the following execution sequence
Trace Description: CTL Counterexample
Trace Type: Counterexample
-> State: 1.1 <-
state1 = n1
state2 = n2
turn = 1
-- specification AG (state1 = t1 -> AF state1 = c1) is true
-- specification AG (state2 = t2 -> AF state2 = c2) is true
The code
MODULE main
VAR
state1: {n1, t1, c1};
ASSIGN
init(state1) := n1;
next(state1) :=
case
(state1 = n1) & (state2 = t2): t1;
(state1 = n1) & (state2 = n2): t1;
(state1 = n1) & (state2 = c2): t1;
(state1 = t1) & (state2 = n2): c1;
(state1 = t1) & (state2 = t2) & (turn = 1): c1;
(state1 = c1): n1;
TRUE : state1;
esac;
VAR
state2: {n2, t2, c2};
ASSIGN
init(state2) := n2;
next(state2) :=
case
(state2 = n2) & (state1 = t1): t2;
(state2 = n2) & (state1 = n1): t2;
(state2 = n2) & (state1 = c1): t2;
(state2 = t2) & (state1 = n1): c2;
(state2 = t2) & (state1 = t1) & (turn = 2): c2;
(state2 = c2): n2;
TRUE : state2;
esac;
VAR
turn: {1, 2};
ASSIGN
init(turn) := 1;
next(turn) :=
case
(state1 = n1) & (state2 = t2): 2;
(state2 = n2) & (state1 = t1): 1;
TRUE : turn;
esac;
SPEC
EF((state1 = c1) & (state2 = c2))
SPEC
AG((state1 = t1) -> AF (state1 = c1))
SPEC
AG((state2 = t2) -> AF (state2 = c2))
Before jumping into the smv model, you need to understand at what level of detail you are interested in modeling the UART component. It can be helpful to first model the component in a different formalism, so that you do not get stuck with syntactical issues. What are the inputs of the component? What are the outputs? Is there internal state? How does the internal state change over time and, in particular, in one step?
If you are familiar with hardware description languages (e.g., Verilog and VHDL), this would be a very good starting point, since a transition in SMV can be seen as a clock tick. If you do not know those languages, you can try to write a piece of software instead; this will help you understand input/outputs of the system, but the translation into SMV will not be so immediate.
For components that are very stateful, manually drawing the corresponding automata might help.

How to randomly assign variables

In Stata, I want to create a new variable with values associated with probabilities from a know distribution.
Say that the distribution pdf looks like:
Blue - .2
Red - .3
Green - .5
I can use code like the following to get the exact distribution as above. First, is there a quicker way to accomplish this?
gen Color = ""
replace Color = "Blue" if _n <= _N*.2
replace Color = "Red" if _n > _N*.2 & _n <= _N*.5
replace Color = "Green" if Color==""
To simulate random draws, I think I can do:
gen rand = runiform()
sort rand
gen Color = ""
replace Color = "Blue" if rand <= .2
replace Color = "Red" if rand > .2 & rand <= .5
replace Color = "Green" if Color==""
Is this technique best practice?
When producing the data, you could use the more efficient in instead of if. But to be honest, I believe the data set would have to be very big for time differences to be perceivable. You can do some experimenting to check for that.
The second issue on random draws is already addressed by a series of posts authored by Bill Gould (StataCorp's president). Some code below with inline comments. You can run the whole thing and check the results.
clear
set more off
*----- first question -----
/* create data with certain distribution */
set obs 100
set seed 23956
gen obs = _n
gen rand = runiform()
sort rand
gen Color = ""
/*
// original
replace Color = "Blue" if _n <= _N*.2
replace Color = "Red" if _n > _N*.2 & _n <= _N*.5
replace Color = "Green" if Color==""
*/
// using -in-
replace Color = "Blue" in 1/`=floor(_N*.2)'
replace Color = "Red" in `=floor(_N*.2) + 1'/`=floor(_N*.5)'
replace Color = "Green" in `=floor(_N*.5) + 1'/L
/*
// using -cond()-
gen Color = cond(_n <= _N*.2, "Blue", cond(_n > _N*.2 & _n <= _N*.5, "Red", "Green"))
*/
drop rand
sort obs
tempfile allobs
save "`allobs'"
tab Color
*----- second question -----
/* draw without replacement a random sample of 20
observations from a dataset of N observations */
set seed 89365
sort obs // for reproducibility
generate double u = runiform()
sort u
keep in 1/20
tab obs Color
/* If N>1,000, generate two random variables u1 and u2
in place of u, and substitute sort u1 u2 for sort u */
/* draw with replacement a random sample of 20
observations from a dataset of N observations */
clear
set seed 08236
drop _all
set obs 20
generate long obsno = floor(100*runiform()+1)
sort obsno
tempfile obstodraw
save "`obstodraw'"
use "`allobs'", clear
generate long obsno = _n
merge 1:m obsno using "`obstodraw'", keep(match) nogen
tab obs Color
These and other details can be found in the four-part series on random-number
generators, by Bill Gould: http://blog.stata.com/2012/10/24/using-statas-random-number-generators-part-4-details/
See also help sample!

How can I filter a TXT file using VB.NET?

I have a txt file that displays the following information returned by AutoCAD:
; IAcadToolbar: An AutoCAD toolbar
; Property values:
; Application (RO) = #<VLA-OBJECT IAcadApplication 00d591b4>
; Count (RO) = 19
; DockStatus (RO) = 4
; FloatingRows = 5
; Height (RO) = AutoCAD: The toolbar is invisible. Please make it visible
; HelpString = "Draw Toolbar\n "
; LargeButtons (RO) = 0
; left = 1310
; Name = "Draw"
; Parent (RO) = #<VLA-OBJECT IAcadToolbars 224a6b04>
; TagString (RO) = "ID_TbDraw"
; top = 646
; Visible = 0
; Width (RO) = AutoCAD: The toolbar is invisible. Please make it visible
; Methods supported:
; AddSeparator (1)
; AddToolbarButton (5)
; Delete ()
; Dock (1)
; Float (3)
; Item (1)
Problem being is that I need to filter it so I am only left with:
; DockStatus (RO) = 4
; left = 1310
; Name = "Draw"
; top = 646
; Visible = 0
They must also remain in the same order:
; DockStatus (RO) = 4 always on top followed by
; left = 1310 and so on, all other information may be discarded.
Dose anybody know how to do this in VB.NET?
Imports System.IO
Imports System.Text
Dim output = New StringBuilder()
' Read the lines from FileName into an array of strings. '
Dim input = File.ReadAllLines(FileName)
For Each line in input
If line.StartsWith("; DockStatus (RO) = ") OrElse
line.StartsWith("; left = ") OrElse
line.StartsWith("; Name = ") OrElse
line.StartsWith("; top = ") OrElse
line.StartsWith("; Visible = ") Then
output.AppendLine(line)
End If
Next
Ok, assuming that you've got a file that you're reading line by line, can't you simply do something like this? (pseudo code only)
string result;
foreach line in linesFromFile
{
if(line.StartsWith("; DockStatus") or
line.StartsWith("; Name = "))
{
result += line;
}
}
What I've presented is not pretty, but might get you started.
i would suggest to put all values in string and then use splitter
then filter out the values you need
and sort them in order you need
then convert them as required in your output format

Group by Pairs all textbox input vb.net 2010

I have a textbox, I want to group all input two by two.
Input: E5D3DFOXJFUIOXZJDFCNIUEBSKDLFJCNESODFKJ
I want to become: E5 D3 DF OX JF UI OX ZJ DF CN IU EB SK DL FJ CN ES OD FK J
How can I do that?
I have this function but it doesn't really work :
For i As Integer = TextBox1.Text.Length - 2 To 2 Step -2
TextBox1.Text = TextBox1.Text.Insert(i, " ")
Next
It gives me something like that :
E5D 3D FO XJ FU IO XZ JD FC NI UE BS KD LF JC NE SO DF KJ
or when the string is too long it's like that :
E5D 3D FO XJ FU IO XZ JD F C NI UE BS K D LF JC NE SO DF KJ
Anyone can help me with this?
Please excuse the language switch to C#. Not particularly elegant, but the following code should work for both even and odd lengths of string
string buffer = String.Empty;
for (int i = 0; i < textBox1.Text.Length; i += 2)
{
// Exclude the case where 1 or 2 remaining chars here (no trailing space)
if (textBox1.Text.Length - i > 2)
{
buffer += textBox1.Text.Substring(i, 2) + " ";
}
else
{
buffer += textBox1.Text.Substring(i);
}
}
textBox1.Text = buffer;
Thanls you for reply, C# or VB.net it's the same :-) in vb2010 it will be :
Dim buffer As String = [String].Empty
For i As Integer = 0 To TextBox1.Text.Length - 1 Step 2
' Exclude the case where 1 or 2 remaining chars here (no trailing space)
If TextBox1.Text.Length - i > 2 Then
buffer += TextBox1.Text.Substring(i, 2) & " "
Else
buffer += TextBox1.Text.Substring(i)
End If
Next
TextBox1.Text = buffer
K.N.A.82.A.C.M