Skip to content

Commit

Permalink
aliases and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephAbbey committed Oct 24, 2022
1 parent bbf6f20 commit a293733
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
9 changes: 7 additions & 2 deletions examples/binary_counter.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ end entity;

architecture scratch of binary_counter is

constant button_tab_c : positive := 1;

signal cnt : integer range 0 to 15;
signal run : std_logic;

alias start is buttons(0);
alias stop is buttons(1);

begin

leds <= to_stdulogicvector(cnt);
Expand All @@ -29,9 +34,9 @@ begin
run <= '0';
cnt <= 0;
else
if buttons(0) = '1' then
if start = '1' then
run <= '1';
elsif buttons(1) = '1' then
elsif stop = '1' then
run <= '0';
end if;
if (run = '1') and (incr = '1') then
Expand Down
2 changes: 1 addition & 1 deletion examples/binary_counter.vhdl.sbd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"blocks":{"languageVersion":0,"blocks":[{"type":"process","id":"avqozk~ylO6~9`OoO-tF","x":463,"y":13,"extraState":{"depCount":1},"fields":{"1":{"id":"5Y9_SLHF8Bd4|n|ymNgJ"}},"inputs":{"body":{"block":{"type":"controls_if","id":"7)2aH8jQ`CxLSWy}UAgr","inputs":{"IF0":{"block":{"type":"logic_rising_edge","id":"0QWfBso?I$XYQ8@mej5}","fields":{"dep":{"id":"5Y9_SLHF8Bd4|n|ymNgJ"}}}},"DO0":{"block":{"type":"controls_if","id":"|iCK:,3?*n,e{^9zO]%y","extraState":{"hasElse":true},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"B=Og_Cm*AO)T}CKQAZXF","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"Db8Ohuaw7PUe(HOT?d:%","fields":{"VAR":{"id":"2G:Z~BB|CAam0[(.o??k"}}}},"B":{"block":{"type":"value_std_logic","id":"HV[(76@,1o#o|NIyg{4L","fields":{"VALUE":1}}}}}},"DO0":{"block":{"type":"variables_set","id":")~8k1(;U?4ft4XPFi{@o","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"Xxc}cOExM?./jx~mV8*O","fields":{"VALUE":0}}}},"next":{"block":{"type":"variables_set","id":"Cf-SgU0h/|v-!Q^9k-b6","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"VALUE":{"block":{"type":"math_number","id":"ma#QQ4qfMTB$a6#(=M?p","fields":{"NUM":0}}}}}}}},"ELSE":{"block":{"type":"controls_if","id":"f-,*:@2#uyD21rpE2f)f","extraState":{"elseIfCount":1},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"2nY3KBpnFQ.2yXV[x68L","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"list_index","id":"B[/K_[ezcB2Qx~37R4M-","fields":{"INDEX":0},"inputs":{"LIST":{"block":{"type":"variables_get","id":",Yo/8Lf=M*CmYrw_OUj(","fields":{"VAR":{"id":"N;q[+Y+(ah3cQgJ:uCc]"}}}}}}},"B":{"block":{"type":"value_std_logic","id":"~(@!41XyTF6o`q~PKqdq","fields":{"VALUE":1}}}}}},"DO0":{"block":{"type":"variables_set","id":"}K[KHLm0?`E~d/(XP1vh","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"UV9mO#P,pc?1F+kN-|~]","fields":{"VALUE":1}}}}}},"IF1":{"block":{"type":"logic_compare","id":"u1MTJoP`db#/(ia?gQFr","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"list_index","id":"ZUV!0KG40./|gDHaQ1dI","fields":{"INDEX":1},"inputs":{"LIST":{"block":{"type":"variables_get","id":"?}{8%#9qAKe{QptQ%H3[","fields":{"VAR":{"id":"N;q[+Y+(ah3cQgJ:uCc]"}}}}}}},"B":{"block":{"type":"value_std_logic","id":":/K-oZGH+Df@l|.cNM7W","fields":{"VALUE":1}}}}}},"DO1":{"block":{"type":"variables_set","id":":2}zwaU/+0U_^^X6SdVZ","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"j]v$V?;+-|3c6IA86}%b","fields":{"VALUE":0}}}}}}},"next":{"block":{"type":"controls_if","id":"+w#Q=H/Nwo|sX*WnGu_7","inputs":{"IF0":{"block":{"type":"logic_operation","id":"ct^,b/*0mfrz)hZ96%SR","fields":{"OPERATION":"and"},"inputs":{"A":{"block":{"type":"logic_compare","id":"ar033LzB:8~Asbfhqc1G","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"14Nh=)5qx3Kp7CnM{_3Q","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}}}},"B":{"block":{"type":"value_std_logic","id":"`Z:vS*c~|rDt;H^|*;6/","fields":{"VALUE":1}}}}}},"B":{"block":{"type":"logic_compare","id":"wjWT^X5D+m=*SCJgao1i","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"w%M}[tqe1tZ~$hGT{Auh","fields":{"VAR":{"id":"vLZ;k9sxoNVKVc?I}kk|"}}}},"B":{"block":{"type":"value_std_logic","id":"ivUlYk}vMq%}C_o|+hpe","fields":{"VALUE":1}}}}}}}}},"DO0":{"block":{"type":"controls_if","id":"02fAV_-?A;IM5h+kf{B4","extraState":{"hasElse":true},"icons":{"comment":{"text":"to prevent natural overflow","pinned":true,"height":80,"width":160}},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"RzN!wxca#NjI~DODa)C=","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"%@),sr{`cx-V9bcvV48S","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}}}},"B":{"block":{"type":"math_number","id":"[BHDfT^}`|fQA`/=mn}5","fields":{"NUM":15}}}}}},"DO0":{"block":{"type":"variables_set","id":":uxhh-O5/%w=;qFRPM3e","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"VALUE":{"block":{"type":"math_number","id":"d7`j(wM,Ck,x#R.h4hX+","fields":{"NUM":0}}}}}},"ELSE":{"block":{"type":"math_change","id":"q$0[C|3apsd3w^;44!%{","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"DELTA":{"shadow":{"type":"math_number","id":"N][!U{H/z_@V-SvgJBKp","fields":{"NUM":1}}}}}}}}}}}}}}}}}}}}}},{"type":"process_direct_set","id":"2WV_x^9Fn^K5~HN@?IpG","x":-12,"y":13,"fields":{"VAR":{"id":"zZ.FFw)dey[B4Vwn^yh`"}},"inputs":{"VALUE":{"block":{"type":"ieee.numeric_std_unsigned_to_stdulogicvector","id":")LdWhR+dpy:CwuP0dPr@","inputs":{"NUM":{"block":{"type":"variables_get","id":"+]QX^TgSSU{H:QQ1jOID","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}}}}}}}}}]},"variables":[{"name":"cnt","id":"gWcVR:~_ouWxb9@{IwGa"},{"name":"run","id":"E0tQW(i(Up||M4R4RYG["},{"name":"clk","id":"5Y9_SLHF8Bd4|n|ymNgJ"},{"name":"reset","id":"2G:Z~BB|CAam0[(.o??k"},{"name":"incr","id":"vLZ;k9sxoNVKVc?I}kk|"},{"name":"buttons","id":"N;q[+Y+(ah3cQgJ:uCc]"},{"name":"leds","id":"zZ.FFw)dey[B4Vwn^yh`"}],"signals":{"cnt":"integer range 0 to 15","run":"std_logic"}}
{"blocks":{"languageVersion":0,"blocks":[{"type":"process","id":"avqozk~ylO6~9`OoO-tF","x":463,"y":13,"extraState":{"depCount":1},"fields":{"1":{"id":"5Y9_SLHF8Bd4|n|ymNgJ"}},"inputs":{"body":{"block":{"type":"controls_if","id":"7)2aH8jQ`CxLSWy}UAgr","inputs":{"IF0":{"block":{"type":"logic_rising_edge","id":"0QWfBso?I$XYQ8@mej5}","fields":{"dep":{"id":"5Y9_SLHF8Bd4|n|ymNgJ"}}}},"DO0":{"block":{"type":"controls_if","id":"|iCK:,3?*n,e{^9zO]%y","extraState":{"hasElse":true},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"B=Og_Cm*AO)T}CKQAZXF","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"Db8Ohuaw7PUe(HOT?d:%","fields":{"VAR":{"id":"2G:Z~BB|CAam0[(.o??k"}}}},"B":{"block":{"type":"value_std_logic","id":"HV[(76@,1o#o|NIyg{4L","fields":{"VALUE":1}}}}}},"DO0":{"block":{"type":"variables_set","id":")~8k1(;U?4ft4XPFi{@o","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"Xxc}cOExM?./jx~mV8*O","fields":{"VALUE":0}}}},"next":{"block":{"type":"variables_set","id":"Cf-SgU0h/|v-!Q^9k-b6","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"VALUE":{"block":{"type":"math_number","id":"ma#QQ4qfMTB$a6#(=M?p","fields":{"NUM":0}}}}}}}},"ELSE":{"block":{"type":"controls_if","id":"f-,*:@2#uyD21rpE2f)f","extraState":{"elseIfCount":1},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"2nY3KBpnFQ.2yXV[x68L","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"{x_Iv}av0ah8zVeQ%V+;","fields":{"VAR":{"id":"r]hfmAi8AfzG[s)uc9w+"}}}},"B":{"block":{"type":"value_std_logic","id":"~(@!41XyTF6o`q~PKqdq","fields":{"VALUE":1}}}}}},"DO0":{"block":{"type":"variables_set","id":"}K[KHLm0?`E~d/(XP1vh","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"UV9mO#P,pc?1F+kN-|~]","fields":{"VALUE":1}}}}}},"IF1":{"block":{"type":"logic_compare","id":"u1MTJoP`db#/(ia?gQFr","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"bjF?^Q8dAC9N)lm6sD/v","fields":{"VAR":{"id":"Mx0l_Ufp-l?o.oic9dG("}}}},"B":{"block":{"type":"value_std_logic","id":":/K-oZGH+Df@l|.cNM7W","fields":{"VALUE":1}}}}}},"DO1":{"block":{"type":"variables_set","id":":2}zwaU/+0U_^^X6SdVZ","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}},"inputs":{"VALUE":{"block":{"type":"value_std_logic","id":"j]v$V?;+-|3c6IA86}%b","fields":{"VALUE":0}}}}}}},"next":{"block":{"type":"controls_if","id":"+w#Q=H/Nwo|sX*WnGu_7","inputs":{"IF0":{"block":{"type":"logic_operation","id":"ct^,b/*0mfrz)hZ96%SR","fields":{"OPERATION":"and"},"inputs":{"A":{"block":{"type":"logic_compare","id":"ar033LzB:8~Asbfhqc1G","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"14Nh=)5qx3Kp7CnM{_3Q","fields":{"VAR":{"id":"E0tQW(i(Up||M4R4RYG["}}}},"B":{"block":{"type":"value_std_logic","id":"`Z:vS*c~|rDt;H^|*;6/","fields":{"VALUE":1}}}}}},"B":{"block":{"type":"logic_compare","id":"wjWT^X5D+m=*SCJgao1i","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"w%M}[tqe1tZ~$hGT{Auh","fields":{"VAR":{"id":"vLZ;k9sxoNVKVc?I}kk|"}}}},"B":{"block":{"type":"value_std_logic","id":"ivUlYk}vMq%}C_o|+hpe","fields":{"VALUE":1}}}}}}}}},"DO0":{"block":{"type":"controls_if","id":"02fAV_-?A;IM5h+kf{B4","extraState":{"hasElse":true},"icons":{"comment":{"text":"to prevent natural overflow","pinned":true,"height":80,"width":160}},"inputs":{"IF0":{"block":{"type":"logic_compare","id":"RzN!wxca#NjI~DODa)C=","fields":{"OP":"EQ"},"inputs":{"A":{"block":{"type":"variables_get","id":"%@),sr{`cx-V9bcvV48S","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}}}},"B":{"block":{"type":"math_number","id":"[BHDfT^}`|fQA`/=mn}5","fields":{"NUM":15}}}}}},"DO0":{"block":{"type":"variables_set","id":":uxhh-O5/%w=;qFRPM3e","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"VALUE":{"block":{"type":"math_number","id":"d7`j(wM,Ck,x#R.h4hX+","fields":{"NUM":0}}}}}},"ELSE":{"block":{"type":"math_change","id":"q$0[C|3apsd3w^;44!%{","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}},"inputs":{"DELTA":{"shadow":{"type":"math_number","id":"N][!U{H/z_@V-SvgJBKp","fields":{"NUM":1}}}}}}}}}}}}}}}}}}}}}},{"type":"process_direct_set","id":"2WV_x^9Fn^K5~HN@?IpG","x":-12,"y":13,"fields":{"VAR":{"id":"zZ.FFw)dey[B4Vwn^yh`"}},"inputs":{"VALUE":{"block":{"type":"ieee.numeric_std_unsigned_to_stdulogicvector","id":")LdWhR+dpy:CwuP0dPr@","inputs":{"NUM":{"block":{"type":"variables_get","id":"+]QX^TgSSU{H:QQ1jOID","fields":{"VAR":{"id":"gWcVR:~_ouWxb9@{IwGa"}}}}}}}}}]},"variables":[{"name":"cnt","id":"gWcVR:~_ouWxb9@{IwGa"},{"name":"run","id":"E0tQW(i(Up||M4R4RYG["},{"name":"clk","id":"5Y9_SLHF8Bd4|n|ymNgJ"},{"name":"reset","id":"2G:Z~BB|CAam0[(.o??k"},{"name":"incr","id":"vLZ;k9sxoNVKVc?I}kk|"},{"name":"buttons","id":"N;q[+Y+(ah3cQgJ:uCc]"},{"name":"leds","id":"zZ.FFw)dey[B4Vwn^yh`"},{"name":"start","id":"r]hfmAi8AfzG[s)uc9w+"},{"name":"stop","id":"Mx0l_Ufp-l?o.oic9dG("}],"signals":{"cnt":"integer range 0 to 15","run":"std_logic"}}
7 changes: 7 additions & 0 deletions examples/binary_counter.vhdl.sbe
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"buttons" : ["in", "std_logic_vector(3 downto 0)"],
"leds" : ["out", "std_logic_vector(3 downto 0)"]
},
"aliases": {
"start": "buttons(0)",
"stop" : "buttons(1)"
},
"constants": {
"button_tab_c": ["positive", "1"]
},
"libraries": {
"ieee": {
"numeric_std_unsigned": "ieee.numeric_std_unsigned.sbi"
Expand Down
3 changes: 3 additions & 0 deletions examples/button_driven.vhdl.sbe
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"buttons" : ["in", "std_logic_vector(3 downto 0)"],
"leds" : ["out", "std_logic_vector(3 downto 0)"]
},
"constants": {
"button_tab_c": ["positive", "1"]
},
"name": "led4_button4"
}
66 changes: 64 additions & 2 deletions scratch-vhdl-vscode/media/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ document.addEventListener('DOMContentLoaded', function () {
Blockly.dialog.setConfirm((m, cb) => confirm(m).then((d) => cb(d ?? false)));

const entityVars = {};
const aliasVars = {};

(() => {
Blockly.fieldRegistry.unregister("field_variable");
Expand All @@ -722,6 +723,7 @@ document.addEventListener('DOMContentLoaded', function () {
super.renderSelectedText_();
if (["variables_get", "variables_set", "variables_set_index", "math_change", "process_direct_set"].indexOf(this.sourceBlock_.type) !== -1) {
if (Object.keys(entityVars).indexOf(this.value_) !== -1) this.sourceBlock_.setColour(20);
else if (Object.keys(aliasVars).indexOf(this.value_) !== -1) this.sourceBlock_.setColour(120);
else this.sourceBlock_.setColour('%{BKY_VARIABLES_HUE}');
}
}
Expand Down Expand Up @@ -766,7 +768,7 @@ document.addEventListener('DOMContentLoaded', function () {
"\n signal " + n + " : " + signals[n] + ";"
).join("");
}
return librariesCode + "\n\n" + entityCode + "\n\n\narchitecture scratch of " + name + " is\n" + gen_signals(signals) + "\n\nbegin\n" + Object.getPrototypeOf(this).finish.call(this, code) + "\n\nend architecture;\n";
return librariesCode + "\n\n" + entityCode + "\n\n\narchitecture scratch of " + name + " is\n" + constantsCode + "\n" + gen_signals(signals) + "\n" + aliasesCode + "\n\nbegin\n" + Object.getPrototypeOf(this).finish.call(this, code) + "\n\nend architecture;\n";
}

VHDLGenerator.scrub_ = function (block, code, opt_thisOnly) {
Expand Down Expand Up @@ -853,6 +855,10 @@ document.addEventListener('DOMContentLoaded', function () {
return [block.getField("VAR").getVariable().name, VHDLGenerator.ORDER_ATOMIC];
}

VHDLGenerator.constant = function (block) {
return [block.getFieldValue("NAME"), VHDLGenerator.ORDER_ATOMIC];
}

VHDLGenerator.variables_set = function (block) {
return block.getField("VAR").getVariable().name + ' <= ' +
VHDLGenerator.valueToCode(block, 'VALUE', VHDLGenerator.ORDER_NONE) + ';\n';
Expand Down Expand Up @@ -898,8 +904,12 @@ document.addEventListener('DOMContentLoaded', function () {
}
};
let signals = {};
let constants = {};
let aliases = {};
let entityCode = "";
let librariesCode = "";
let constantsCode = "";
let aliasesCode = "";

Blockly.serialization.registry.register(
'signals',
Expand Down Expand Up @@ -961,18 +971,70 @@ document.addEventListener('DOMContentLoaded', function () {
).join(";") +
"\n );\nend entity;";
}
function generateAliases() {
Blockly.Events.disable();
for (const n in aliases) {
aliasVars[ws.createVariable(n).id_] = n;
}
// rerender all variable fields
ws.getAllBlocks().forEach((b) => b.inputList.forEach((c) => c.fieldRow.forEach((d) => d instanceof Blockly.FieldVariable && d.renderSelectedText_())));
Blockly.Events.enable();

aliasesCode =
Object.keys(aliases).map((n) =>
"\n alias " + n + " is " + aliases[n] + ";"
).join("");
}
window.addEventListener('message',
(message) => message.data.type == "contentUpdate" && (
Blockly.Events.disable(),
Blockly.serialization.workspaces.load(JSON.parse(message.data.body), ws, { registerUndo: false }),
Blockly.Events.enable(),
generateEntity()
generateEntity(), generateAliases()
));
window.addEventListener('message',
async (message) => {
if (message.data.type == "entity") {
entity = JSON.parse(message.data.body);
name = entity.name || message.data.file_name;
constants = entity.constants || constants;
aliases = entity.aliases || aliases;

if (entity.constants) {
Blockly.defineBlocksWithJsonArray([{
type: "constant",
message0: "%1",
args0: [
{
"type": "field_dropdown",
"name": "NAME",
"options": Object.keys(constants).map((a) => [a, a])
},
],
colour: 250,
output: null
}]);
const tb = ws.getToolbox().toolboxDef_;
tb.contents = [
...tb.contents,
{
kind: "category",
colour: 250,
contents: Object.keys(constants).map((a) => ({
kind: "block",
type: "constant",
fields: {
"NAME": a
}
})),
name: "Constants"
}
];
ws.updateToolbox(tb);
constantsCode = Object.keys(constants).map((n) =>
"\n constant " + n + " : " + constants[n][0] + " := " + constants[n][1] + ";"
).join("");
}

if (entity.libraries) {
function libraryDef(def) {
Expand Down

0 comments on commit a293733

Please sign in to comment.