nand2tetris CPU.cmp line 17 problem; outM/(RAM[A]) decrements twice with MD=D-1 instruction; - nand2tetris

I'm trying to work through the CPU.cmp file, to write out the instructions and see if what's written in CPU.cmp makes sense.
On line 17 (time 8)
|time| inM | instruction |reset| outM |writeM |addre| pc |DRegiste|
|6+ | 0|0000001111101001| 0 |*******| 0 | 1000| 6| 11111 |a #1001
|7 | 0|0000001111101001| 0 |*******| 0 | 1001| 7| 11111 |a
|7+ | 0|1110001110011000| 0 | 11110| 1 | 1001| 7| 11110 |c MD = D-1; null
|8 | 0|1110001110011000| 0 | 11109| 1 | 1001| 8| 11110 |c
As you can see, the value of the D register decrements by 1 from (decimal) 11111 to 11110, and the value of the outM reflects that. However, then outM decrements again, to 11109. Why does it do that? The instruction is MD = D-1, so it should decrement D reg once, and store the value in two locations. How does it happen that RAM[A] and D end up with different values?
I expected them to be the same...

At tick 7+ CPU has to perform instruction MD=D-1 as you figured out already.
At this point D=11111.
Instruction is D-1 and the CPU will internally calculate this value,
so outM( output bus, not actual memory) is D-1 (11110) and register D = 11110 as well. At tock 8 write to memory happens so 11110 is saved at address in register A.
CPU still calculates D-1 so outM = 11109, completely correct behaviour of CPU, we don't use this value but it represents result of last instruction (still D-1).
To expand on ticks and tocks:
tick is the 1 on clock, it's the part of the cycle where all outputs and registers have time to change and stabilize their internal values. Registers being loaded output their changing internal state immediately as input changes.
tock is the part that clocked parts start emitting their internal state and won't change their internal values.
That's why DRegister has the same value as outM on tick, ALU feeds D-1 as their value. Then comes tock and D starts emitting its new value(d-1) and ALU calculates its new value (you see it as D-2). D doesn't change because in tock phase clocked parts don't change their value.
Also to answer
How does it happen that RAM[A] and D end up with different values
They don't, outM and DRegister end up with different values. outM is not clocked, it's direct output of ALU, which calculates out(D) - 1. DRegister has the same ALU output as an input but it won't update its value because of clock phase.

Related

CUDD: Manipulation of BDDs

I'm working with CUDD C++ interface (https://github.com/ivmai/cudd) but there is almost no information about this library. I would like to know how to remove one variable according to its value.
For example, I have now the next table stored in a bdd:
|-----|-----|-----|
| x1 | x2 | y |
|-----|-----|-----|
| 0 | 0 | 1 |
|-----|-----|-----|
| 0 | 1 | 1 |
|-----|-----|-----|
| 1 | 0 | 1 |
|-----|-----|-----|
| 1 | 1 | 0 |
|-----|-----|-----|
And I want to split the previous table in two separate bdds according to the value of x2 and remove that node afterwards:
If x2 = 0:
|-----|-----|
| x1 | y |
|-----|-----|
| 0 | 1 |
|-----|-----|
| 1 | 1 |
|-----|-----|
If x2 = 1:
|-----|-----|
| x1 | y |
|-----|-----|
| 0 | 1 |
|-----|-----|
| 1 | 0 |
|-----|-----|
Is it possible?
The reason that there is almost no documentation on the C++ interface of the CUDD library is that it is just a wrapper for the C functions, for which there is plenty of documentation.
The C++ wrapper is mainly useful for getting rid of all the Cudd_Ref(...) and Cudd_RecursiveDeref(...) calls that code using the C interface would need to do. Note that you can use the C interface from C++ code as well, if you want.
To do what you want to do, you have to combine the Boolean operators offered by CUDD in a way that you obtain a new Boolean function with the desired properties.
The first step is to restrict s to the x=0 and x=1 case:
BDD s0 = s & !x;
BDD s1 = s & x;
As you noticed, the new BDDs are not (yet) oblivious to the value of the x variable. You want them to be "don't care" w.r.t to the value of x. Since you already know that x is restricted to one particular value in s0 and s1, you can use the existential abstraction operator:
s0 = s0.ExistAbstract(x);
s1 = s1.ExistAbstract(x);
Note that x is used here as a so-called cube (see below).
These are now the BDDs that you want.
Cube explanation: If you abstract from multiple variables at the same time, you should compute such a cube from all the variables to be abstracted from first. A cube is mainly used for representing a set of variables. From mathematical logic, it is known that if you existentially or universally abstract away multiple variables, then the order to abstracting away these variables does not matter. Since the recursive BDD operations in CUDD are implemented over pairs (or triples) of BDDs whenever possible, CUDD internally represents a set of variables as a cube as well, so that an existential abstraction operation can just work on (1) the BDD for which existential abstraction is to be performed, and (2) the BDD representing the set of variables to be abstracted from. The internal representation of a cube as a BDD should not be of relevance to a developer just using CUDD (rather than extending CUDD), except that a BDDD representing a variable can also be used as a cube.
An approach using the Cython bindings to CUDD of the Python package dd is the following, which uses substitution of constant values for variable x2.
import dd.cudd as _bdd
bdd = _bdd.BDD()
bdd.declare('x1', 'x2')
# negated conjunction of the variables x1 and x2
u = bdd.add_expr(r'~ (x1 /\ x2)')
let = dict(x2=False)
v = bdd.let(let, u)
assert v == bdd.true, v
let = dict(x2=True)
w = bdd.let(let, u)
w_ = bdd.add_expr('~ x1')
assert w == w_, (w, w_)
The same code runs in pure Python by changing the import statement to import dd.autoref as _bdd. The pure Python version of dd can be installed with pip install dd. Installation of dd with the module dd.cudd is described here.

Is there a way to make a function node override another function node?

I have a function node comparing to values in my node-red and outputting the greater one. I want it to:
Compare the 2 values and output the greater one.
Recompare the 2 values again after 5 seconds.
If the first value is greater, carry on outputting it else enter another function node.
It should ignore the first function node and carry on with the second function node.
Note: The 2 values are being constantly fed by a serial input. My problem is that it keeps passing into the first function then to the second. I want it to skip the first function node when the requirements are met and go to the second function node directly.
Thank you.
You can set a variable in the flow or global context in the first function node and then you can check that value in a switch node to determine what branch to follow.
------------- ----------- -------------- --------------
| serial |----->| switch |----->| function 1 |---->| function 2 |
------------- | |---| -------------- | --------------
----------- | |
|------------------|

SSIS Surrogate Key incrementation

I'm using SSIS to create a star schema for a data warehouse with surrogate keys (sg).
My process goes like this:
find max sg (using SQL)
in data flow: data source-> c# script that adds +1 to the max sg -> write to destination.
Now, with fixed dimensions it works without problems. Every added row gets the sequential sg.
However when I use the Slowly Changing Dimension and historically updating a row I get the following:
sg_key | name | city | current_row
1 | a | X | true
2 | b | Y | true
3 | c | Z | false
4 | d | H | true
7 | c | T | true
Now, correct me if I'm wrong but I always thought SSIS is pushing one row at a time through all the flow tasks, but it looks like it first generates ALL the sg_keys for all the rows and then sends the updated row through the flow.
Do I understand how SSISworks in a wrong way? How can I fix it?
Cheers,
Mark.
If you use SQL Server as a destination, why didn't use an IDENTITY Column? (instead of a C# script)
https://msdn.microsoft.com/en-us/library/ms186775.aspx
Identity will automatically increment your column when you insert a new row. If you don't update this column, the value will not change.
Arnaud

Unique combinations of variables in Stata

I need assistance with getting a Stata code that can get me unique combinations of varibles. I have 7 variables and I need to run a code that can give me a unique combination of all of these variables. Every row will be a unique combination of all 7 variables.
An example:
V1: A, B, C
V2: 1, 2, 3
A1 A2 A3, B1 B2 B3, C1 C2 C3
Unique combination of all variables - total 9 combinations.
I have 15000 observations. I got a code in R but R won't get the output on a large data (memory error). I want to get this in Stata.
It is not especially clear what you want created or done. There is no code here, not even R code showing how what you want is done in R. There is no reproducible example.
You might want to check out egen, group(). (A previous answer to this effect from #Dimitriy V. Masterov, an experienced user of Stata, was twice incorrectly deleted as spam, presumably by people not knowing Stata.)
Alternatively, try installing groups from SSC.
UPDATE: The answer sounds more like fillin. For "unique" read "distinct".
Bit of a late response, but I just stumbled across this today. If I understand the question, Something like this should do the trick, although I'm not sure it's easily applied to more complex data or if this would even be the best way...
* Create Sample Data
clear
set obs 3
gen str var1 = "a" in 1
replace var1="b" in 2
replace var1="c" in 3
gen var2= _n
* Find number of Unique Groupings to set obs
by var1 var2, sort: gen groups=_n==1
keep if groups==1
drop groups
di _N^2
set obs 9
* Create New Variable
forvalues i = 4(3)9 {
forvalues j = 5(3)9 {
forvalues k = 6(3)9 {
replace var1="a" if _n==`i'
replace var1="b" if _n==`j'
replace var1="c" if _n==`k'
}
}
}
sort var1
egen i=seq(), f(1) t(3)
tostring i, replace
gen NewVar=var1+i
list NewVar
+--------+
| NewVar |
|--------|
1. | a1 |
2. | a2 |
3. | a3 |
4. | b1 |
5. | b2 |
|--------|
6. | b3 |
7. | c1 |
8. | c2 |
9. | c3 |
+--------+
Unfortunately as far as I know, there is no easy way to do this - it will require a fair amount of code. Although, I saw another answer or comment that mentioned cross which could be very useful here. Another command worth checking out is joinby. But even with either of these methods, you will have to split your data into 7 different sets based on the variables you want to 'cross combine'.
Anyway, Good Luck if you haven't yet found your solution.
If you just want the combination of that 7 variables, you can do it like this:
keep v1 v2 v3 v4 v5 v6 v7
duplicates drop
list
Then you will get the list of unique combinations of those 7 variables. You can save the file with different name from the original dataset. Please make sure that you do not save the dataset directly. Otherwise you will lose your original data.

Converting processes from mysql to Redis

I'm coming from mysql, trying to wrap my head around redis. Some things were very obvious but a couple have me stumped. How would you go about implementing such things in redis?
First
I have a sort of first come/first serve reservation system. When a user goes to a specific page, it queries the table below and returns the first badge where reservedby = 0, it then updates reservedby with the users id. If the user doesn't complete the process within 15 minutes, reservedby is reset to 0. If the user completes the process, I delete the row from the table and store the badge with the user data. Order is important, the higher on the list a badge is, the better, so if I were to remove it instead of somehow marking it reserved, it would need to go back in on the top if the process isn't completed with 15 minutes.
id | badge | reservedby
------------------------
240 | abc | 4249
241 | bbb | 0
242 | rrr | 0
Second
I have a set of data that doesn't change very often but is queried a lot. When a page loads, it populates a select box with each color, when you select a color, the corresponding sm and lg are displayed.
id | color | sm | lg
---------------------------
1 | blue | 1 | 5
2 | red | 3 | 10
3 | yellow | 7 | 8
Lastly
As far as storing user data, what I'm doing is INCR users and then taking that value and hmset user:<INCR users value> badge "aab" joindate "10/30/2013" etc, is that typically how it should be done?
in reversed order
yes thats how you increment IDs in Redis, there is no automated feature.
depending on how frequent does the table change, if its once a month consider serving a static json file to client and let client-side deal with the rest of the code.
consider using a ZSET and try to keep unique values at scores, OR a LIST with json values.
IMHO your reservation system internals kind of sucks, i could easily reserve your sites offers simply , by sending multiple http requests, other production websites have a limited offer and the user who pays first has the room, until the payment is processed on success it just keeps going, on failure it reverts back.