Category: Uncategorized

By Reference vs By Pointer Dependency Injection C++

By Reference DI is safer because reference cannot be Null, but it has some issues: initialization, copying, and assignment problem. Something that I’m currently experience now.

By Pointer DI has no issues above, but by using pointer reference you will need to watch some issues like object ownership (where Smart Pointer is better), and null-value (always remember to assert dependencies != nullptr)

My Git Command Tricks

Temporarily switch to a certain commit

Make sure git status is clean. Jump to a specific git commit:
git checkout commit_hash

Example:
git checkout 286202caa31b61c2182209f37f8069a0b60fb942

Back to the original commit:
git checkout branch_name

Example:
git checkout master

Perlukah Membuat Digital Module pada FPGA?

Pada paper design-of-an-fpga-based-vlc-system telah disebutkan bahwa digital module diimplementasikan agar sinyal MII dapat diubah menjadi encoded signal yang siap ditransmisikan yang sesuai dengan standar IEEE 802.3 Ethernet. Pada gambar 1, diperlihatkan bahwa digital module merupakan bagian dari PHY layer sistem.

blokdiagram

Kabel ethernet pada sambungan internet sebetulnya merupakan physical layer juga. Pada titik ini, MAC layer sudah ada di bagian MAC chip di router/switch ethernet. PHY layer juga sudah diimplementasikan pada router/switch. Sehingga sinyal kabel ethernet pada dasarnya adalah sinyal luaran dari blok biru digital module di atas.

Kecuali capstone design akan menggunakan USB-to-MII sebagai sumber koneksi, kita sudah tidak perlu lagi untuk membuat digital module dan MAC layer. Sayangnya, pada posisi tersebut kita tidak lagi membutuhkan FPGA. FPGA sama sekali tidak digunakan.

Terdapat paper lain (ethernet-signal-transmission-via-vlc-lighting)  yang membahas transmisi sinyal ethernet melalui VLC. Di dalamnya hanya terdapat analog circuit yang menerima input berupa sinyal ethernet, dan output dihubungkan ke AFE. Perlu diketahui pula bahwa sistem pada paper tersebut adalah full-duplex, yaitu komunikasi dua arah.

twoway

Media Independent Interface (MII)

Media Independent Interface (MII)

Pada paper design-of-an-fpga-based-vlc-system diuraikan cara interfacing antara PC dengan Analog Front End (AFE) menggunakan chip USB-to-Ethernet. Chip yang digunakan menyediakan pin untuk sinyal MII. Di bawah ini adalah blok diagram sistem dari paper di atas:
blokdiagram

Sementara Board DE2-115 menyediakan chip PHY untuk interfacing ethernet ke FPGA:

blokdiagram2Chip tersebut menyediakan pin MII pula. Sehingga untuk proyek capstone design ini akan digunakan diagram blok sebagai berikut:

untitled-diagram-2

Dikutip dari laman wikipedia, MII memiliki sinyal transmitter dan receiver sebagai berikut:

Transmitter signals

  • TX_CLK Transmit clock (PHY to MAC)
  • TXD0 Transmit data bit 0 (MAC to PHY) (transmitted first)
  • TXD1 Transmit data bit 1 (MAC to PHY)
  • TXD2 Transmit data bit 2 (MAC to PHY)
  • TXD3 Transmit data bit 3 (MAC to PHY)
  • TX_EN Transmit enable (MAC to PHY)
  • TX_ER Transmit error (MAC to PHY, optional)

Receiver signals

  • RX_CLK Receive clock (PHY to MAC)
  • RXD0 Receive data bit 0 (PHY to MAC) (received first)
  • RXD1 Receive data bit 1 (PHY to MAC)
  • RXD2 Receive data bit 2 (PHY to MAC)
  • RXD3 Receive data bit 3 (PHY to MAC)
  • RX_DV Receive data valid (PHY to MAC)
  • RX_ER Receive error (PHY to MAC)
  • CRS Carrier sense (PHY to MAC)
  • COL Collision detect (PHY to MAC)

Interpreter Bahasa Brainfuck Dalam Bahasa C

Brainfuck? Jangan salah paham dulu, Brainfuck adalah sebuah bahasa pemrograman esoteric yang minimalis. Bahasa Brainfuck hanya memiliki delapan perintah yaitu , . [ ] < > + -. Sebetulnya sudah banyak juga sih yang menulis compiler dan interpreter untuk bahasa ini, tapi saya ingin membuatnya sendiri karena tertarik perintahnya yang sedikit, hihi.

Untuk implementasi interpreter Brainfuck, saya memutuskan menggunakan Bahasa C sebagai permulaan. Di bawah ini adalah kode C untuk interpreter Brainfuck, kurang dari 170 baris, dan itu belum dioptimasi! 😛 Setelah di-compile interpreternya dapat menerima file program Brainfuck. Kalau malas compile sendiri, saya juga sudah siapkan executable untuk Windows di link github yang saya sediakan di bagian bawah. 🙂
https://github.com/Rosmianto/Brainfuck-interpreter

[code language=”cpp” collapse=”true”]
#include &lt;stdio.h&gt; // for basic I/O operation.
#include &lt;stdlib.h&gt; // for memory management.

typedef struct stack {
int value;
struct stack *next;
} stack_node_t;

typedef struct {
stack_node_t *top;
} stack_t;

int pop(stack_t *s);
void push(stack_t *s, int i);
char *bracemap(char *program);
void loadBFtoMemory(FILE *handle);
void process(char c);

const int memorySize = 30000;
char *cell;
int currentCell = 0;
int InstructionPointer = 0;
int i, charCount;
FILE *hInput; // Handle to File object.
char *programArray; // Cleaned up Brainfuck program.
char *braces; // Storage of braces pair location.
char *bracesStack; // temporary array for braces stack.
char _temp;

int main(int argc, char *argv[])
{
// Make sure file path always be specified.
if(argc &lt; 2)
return 0;

cell = calloc(memorySize, sizeof(char));

hInput = fopen(argv[1], &quot;r&quot;);

loadBFtoMemory(hInput);

braces = bracemap(programArray);

while(InstructionPointer &lt; charCount)
{
process(programArray[InstructionPointer]);
InstructionPointer++;
}

// printf(&quot;\n&quot;);

// for(i = 0; i &lt; 20; i++)
// printf(&quot;%d|&quot;, cell[i]);

// printf(&quot;\n&quot;);

free(programArray);
fclose(hInput);
return 0;
}

char *bracemap(char *program)
{
int i, start;
char *temp = calloc(charCount, sizeof(char));
stack_t tempStack = {NULL};

for(i = 0; i &lt; charCount; i++)
{
if(programArray[i] == ‘[‘)
{
push(&amp;tempStack, i);
start = i;
}
else if(programArray[i] == ‘]’)
{
int start = pop(&amp;tempStack);
temp[start] = i;
temp[i] = start;
}
}
return temp;
}

void process(char c)
{
switch(c)
{
case ‘+’:
cell[currentCell]++;
break;
case ‘-‘:
cell[currentCell]–;
break;
case ‘&gt;’:
currentCell++;
break;
case ‘&lt;’:
currentCell–;
break;
case ‘.’:
printf(&quot;%c&quot;, cell[currentCell]);
break;
case ‘,’:
cell[currentCell] = (char)getchar();
break;
case ‘[‘:
InstructionPointer = (cell[currentCell] == 0) ? braces[InstructionPointer] : (InstructionPointer);
break;
case ‘]’:
InstructionPointer = (cell[currentCell] == 0) ? (InstructionPointer) : braces[InstructionPointer];
break;
default: // Ignore other characters.
break;
}
}

void loadBFtoMemory(FILE *handle){
char c;
charCount = 0;
programArray = (char*) malloc((charCount + 1) * sizeof(char));
if(programArray == NULL)
printf(&quot;Memory allocation failed.&quot;);
else
{
c = fgetc(hInput);
while(c != EOF)
{
switch(c)
{
case ‘+’:
case ‘-‘:
case ‘&gt;’:
case ‘&lt;’:
case ‘.’:
case ‘,’:
case ‘[‘:
case ‘]’:
programArray[charCount++] = c;
realloc(programArray, (charCount + 1) * sizeof(char));
default:
break;
}
c = (char)fgetc(hInput);
}
}
}

int pop(stack_t *s){
stack_node_t *free_node;
int c;

free_node = s-&gt;top;
c = free_node-&gt;value;
s-&gt;top = free_node-&gt;next;
free(free_node);

return c;
}

void push(stack_t *s, int c){
stack_node_t *new_node;
new_node = (stack_node_t *) malloc(sizeof(stack_node_t));
new_node-&gt;value = c;
new_node-&gt;next = s-&gt;top;

s-&gt;top = new_node;
}
[/code]

Implementing MIPS Processor with VHDL

Implementing MIPS Processor with VHDL

Warning my lovely readers! The following will be a very technical article, but still, I can’t prevent your inner desire to keep reading this great article. 🙂

In my lab course on computer architecture, implementing processor in VHDL is one thing every electrical engineering student have to face. You know, it is like building your own Intel Core i7 yourself, but of course with much much much simplified processor architecture.

One of such thing is MIPS32, MIPS stands for Microprocessor without Interlocked Pipeline Stages. Cool huh?

MIPS is commonly be used for teaching processor architecture in not so great details. There are several stages every processor should have: Instruction Fetch, Instruction Decoding, Executing, Memory access, and write back. (are you confused? I’ve told you this will be very technical, it’s your fault. Keep reading!)

If you curious in the detail of the assignment, you should download the lab guide here (http://el3111.bagus.my.id/modul-praktikum/) unfortunately, it’s written in Indonesian only. No translation!

Instead of building our own hardware by connecting the individual physical logic gates, we’re implementing this MIPS processor in FPGA by writing VHDL code. Personally, I really enjoyed this lab course, I learned a lot when I was doing something. You know, I’m a kinesthetic learner. So I learn more and better when my hands grab something ‘real’.

As a bonus, here a screenshot of VHDL code I wrote days ago.

Capture
This is Arithmetic and Logical Unit component I wrote days ago. You really should not too hard reading the code above!

Have a nice weekend guys. 🙂

(cover image from extremetech.com)

Convert ATX PSU to Workbench Power Supply: PowerBench!

Okay, I have been researching a lot for converting my old Power Supply Unit in ATX form to Lab Bench Power Supply. I really need a Power Supply for my electronic projects.

Power Supply to be built:

I am going to build this. (source: www.tti-test.com)
I am going to build this. (source: www.tti-test.com)

Well, not really.

I am going to convert this old ATX PSU:

ATX PSU (source: www.atxpowersupplies.com)
ATX PSU (source: www.atxpowersupplies.com)

To Workbench Power Supply, with additional 7-segments. And I’m going to add a, oh wait a minute! I found this link https://okelectronic.wordpress.com/tag/atx-workbench-power-supply/

Oh well! That surpasses my article in any aspects! So, I just gonna read that anyway.

Roll Your Own RFID Tags

Proyek keren lainnya yang mengimplementasikan ATTiny85. Proyek ini mengenai RFID yang dibuat sendiri, tentu saja coil juga harus digulung sendiri. Tanpa memakai tenaga baterai apapun. ATTiny85 tipe SOIC (tipe Surface Mount Device) ditenagai oleh arus yang dibangkitkan oleh coil yang terpapar sinyal elektromagnetik dari komunikasi yang dilakukan. Cool!

Building an Arduino out of Paper

Building an Arduino out of Paper

Paperduino, Arduino yang dirakit diatas kertas, microcontroller memakai ATTiny85 8-pin. ATTiny85 ini murah, sekitar 25-35ribu rupiah, dengan 8 KB Flash memory.

Dimana ya USB Controllernya? Hmm, menurut saya USB Controller ditangani microcontroller, dengan catatan bootloader sudah di-burn ke ATTiny85. Tak yakin, masih perlu diteliti lagi.