# Lexical Conventions
### ==Comment==
> [color=pink] // This is a one-line comment .
>
> [color=pink] /*
> Hello ! Everyone ~
> If there are difficulties u encountered ,
> plz check FAQ first . Ty !!
> This is a multiple line comment .
> */
---
### ==Operators==
> unary, i.e., a = ~b; // ~ is a unary operator . [color=pink]
> binary, i.e., a = b && c; // && is a binary operator .
> ternary, i.e., a = b ? c : d; // ? is a ternary operator .
---
### ==Arithmetic Operators==
> Unary (one operand) : `+`, `-`
> Binary (two operands) : `+`, `-`, `*`, `/`, `%`[color=pink]
> Examples :
> * A + B; // 2'b00+2'b01
> * A – B; // 2'b10-2'b01
> * -A; // 2'b01(1) -> 2'b11(-1)
---
### ==Bitwise Operators(Logic binary)==
> Unary : `~`
> Binary : `&`, `|`, `^`, `~^`, `^~` [color=pink]
> Examples : // A = 4'b0101; //B = 4'b1011;
>
> * ~A; // ~A = 4'b1010;
> * A&B; // A&B = (4'b1010)&(4'b1011) = 4'b1010;
> * A|B; // A|B = (4'b1010)|(4'b1011) = 4'b1011;
> * A^B; // A^B = (4'b1010)^(4'b1011) = 4'b0001;
---
### ==Reduction Operators(Logic unary)==
>Unary : `&`, `~&`, `|`, `~|`, `^`, `~^`, `^~`[color=pink]
>Examples : // A = 4'b0101;
>
> * &A; // &A = 1'b0; //即將 A 中的 4 個 bit 做 and
> // eg : A[0] & A[1] & A[2] & A[3]
> * |A; // |A = 1'b1; //即將 A 中的 4 個 bit 做 or
> // eg : A[0] | A[1] | A[2] | A[3]
---
### ==Logical Operators==
>Unary : `!`
>Binary : `&&`, `||` [color=pink]
>Examples : // A = 4'b0101; // B = 4'b1011;
>
> * ((A`==`4'b0101) && (B==4'b1111)) = 1'b0;
> * !(A==4'b0101 ) = 1'b0;
> * !(B==4'b1111 ) = 1'b1;
---
### ==Equality Operators==
>Binary : `==`, `!=`, `===`, `!==`[color=pink]
>說明:`===`, `!==` 是不能合成的。
> - 因為現實生活中,訊號只有 0、1 及 high z (高阻抗)。所以,我們在 RTL Code 中雖然可以模擬 high z 和 <font color=RED>x (unknown value,不是 Don't care)</font>,但現實生活中卻無法比較這兩個值,因此,是無法合成的語法。
> - 補充:我們在 K-Map 中教 don't care (以 x 代表),但這不是我們模擬電路所出現的 x,x 代表 unknown value, wave form 中會出現紅色的部分,表示現在無法判別此訊號是 0 還是 1 (因為訊號一直在變動)。
>
>Examples :
>|`==`範例|0|1|z|x|
>|---|---|---|---|---|
>|0|1|0|x|x|
>|1|0|1|x|x|
>|z|x|x|x|x|
>|x|x|x|x|x|
>
>|`===`範例|0|1|z|x|
>|---|---|---|---|---|
>|0|1|0|0|0|
>|1|0|1|0|0|
>|z|0|0|1|0|
>|x|0|0|0|1|
---
### ==Relational Operators==
>Binary : `!=`, `<`, `<=`[color=pink]
>條件式,與 c 的一般數學式一樣。
>Examples :
>
> * A>=4'b0001;
> * B==4'b1111;
---
### ==Shift Operators==
>有兩種位移,一種是邏輯位移 `>>`, `<<`,一種是算數位移 `>>>`, `<<<`。
兩者差異在算數位移需考慮 MSB 的值,若為1(負數),也會補 1 在 MSB 的位置上,而邏輯位移則只會補 0 在 MSB 上。[color=pink]
>說明:若一個變數被宣告為 signed,則變數的 MSB 會決定正負(1為負、0為正,值則會用二補數的方式來決定大小和正負。
>[二補數相關資料 from wiki](https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8)
>
>Examples : // B = 4'b1011; 假設 B 已宣告是 signed。 // assume wire [3:0] C ;
>
> * C=B>>1'b1; // C = 4'b0101;
> * C=B>>>1'b1; // C = 4'b1101 ;
---
### ==Conditional Operators==
>Ternary : `?:`
>選擇功用,可以將其想成 if else 來思考,方式是 condition ? outcome1 : outcome2;[color=pink]
>Examples : // A = 4'b0101; // B = 4'b1011;
>
> * C=(A>B)?A:B; // C = 4'b1011; 條件成立選擇左邊,反之。
---
### ==Concatenation and Replication Operators==
> 符號 : `{bits, bits,...}`[color=pink]
>Examples : // A = 4'b0101; // B = 4'b1011;
>
> * C={A, B}; // C=8'b0101_1011;
> * D={2'b00, A}; //D=6'b0001_01;
> * E={3{A}}; // E=12'b0101_0101_0101;
> * F={3{3'b110}}; //F=9'b1101_1011_0;
---
# [Home Page:hatched_chick: ](https://hackmd.io/s/BkYeCF5Og)