วันจันทร์ที่ 25 เมษายน พ.ศ. 2559

Source Code ของการคำนวณวิเคาระห์ จำนวนและแจกแจงคำสั่ง MIPS onstruction

Source Code ของการคำนวณวิเคาระห์ จำนวนและแจกแจงคำสั่ง MIPS onstruction

โปรแกรมที่ใช้เขียน คือ : NetBeans IDE 8.0.2

SOURCE CODE

package command;
import java.io.*;
import java.util.Scanner;
public class Command {
 public static void main(String[] args) {
 // call input
 File input = new File("C:\\Users\\Administrator\\Documents"
 + "\\NetBeansProjects\\Wordcommand\\assemblyCode.txt");
try {
    Scanner sc = new Scanner(input);
    String s;
    // คำสั่งที่มีใน code assembly
    final String[] wordsCommand = {"LW","ADDIU","SW","NOP","ADDU"
        ,"JAL","BNE","SLLV","LUI","JR"
        ,"SRAV","ANDI","XOR","AND","OR"};

    //สร้างตัวนับคำเป็น array ความยาวเท่ากับarray ใน wordsCommand
    double countWords[] = new double[wordsCommand.length];
    int AllCountCommand = 0;
    while(sc.hasNext()){
        s = sc.next();

        /*
         *วน loop ใน array wordsCommand หาว่า ใน txt.(assembly code)
         *มีค่า ที่ตรงกับใน array wordsCommand รึป่าว
         */
        for(int i = 0 ; i < wordsCommand.length ; i=i+1){
            if(s.equals(wordsCommand[i])){
                // นับคำ ที่เหมือนในแต่ละindex ใน array wordsCommand
                countWords[i]=countWords[i]+1;
                AllCountCommand=AllCountCommand+1; // นับคำที่ตรงกันทั้งหมด
            }
        }
    }
    System.out.println("Command Assembly has :");

    // Show Output And Compute Percent
    for(int i = 0 ; i < wordsCommand.length ; i=i+1){
        double percent = (countWords[i]/AllCountCommand)*100;
        System.out.print(wordsCommand[i]+" ="+countWords[i]+"(");
        System.out.printf("%.2f",percent);
        System.out.print("%"+")");
        System.out.println();
    }
    System.out.println("All command ="+AllCountCommand);
} catch(Exception ex) {
}
 }
}


ผลการทำงานของโปรแกรม 



วันศุกร์ที่ 4 มีนาคม พ.ศ. 2559

การออกแบบวงจรหารด้วยภาษา VHDL

การออกแบบวงจรหาร
Cr. http://cpre.kmutnb.ac.th/esl


Cr. http://cpre.kmutnb.ac.th/esl


Simulation




หลักการ ให้ r เป็น a ถ้า a น้อยกว่า b จะ shift b ไปทางขวา และ shift left q และ ให้ 0 ไว้ท้าย ถ้า a มากว่าเท่ากับ b จะเอา b ลบ r และ shift left q และ ใส่ 1 ไว้ท้าย และ shift right b เพื่อเลื่อนหลัก

Input :
    clk สัญญาณสาหรับควบคุมการทางานของของระบบ
    a ตัวตั้งขนาด 32 bit
    b ตัวหารขนาด 16 bit
    start ขา บังคับการทางาน เมื่อให้เป็น 1 จะเริ่มการหาร

Output:
   q เป็น ผลหารขนาด 16 bit
   r เป็น เศษผลหารขนาด 16 bit
   done ตัวบอกสถานะการทางานการหาร
Division Code VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;
use IEEe.STD_LOGIc_ARITH.ALL;
use IEEE.std_logic_unsigned.all;

entity division is
generic (WIDTH :integer := 32);
 port ( clk : in std_logic;
   START : in std_logic;
   a : in std_logic_vector(WIDTH-1 downto 0);
   b : in std_logic_vector((WIDTH/2)-1 downto 0);
DONE : out std_logic;
   q,r : out std_logic_vector((WIDTH/2)-1 downto 0)
   
 );
end division;

architecture Behavioral of division is
signal temp_b : std_logic_vector(WIDTH-1 downto 0);
signal temp_q : std_logic_vector((WIDTH/2)-1 downto 0);
signal temp_r : std_logic_vector(WIDTH-1 downto 0);
signal done_1 : std_logic := '1';  -- เอาไว้ตรวจสอบดูว่าหารเสร็จหรือยัง
signal count : integer := 0;

begin
 process(clk)
  begin
   if rising_edge(clk) then
    done <= done_1;
    if START = '1' then
     temp_r <= a;
     temp_b <= b&conv_std_logic_vector(0,WIDTH/2);--conv_std_logic_vector แปลงฐาน10 เป็นฐาน2
     done_1 <= '0';  -- done_1เป็น 0 คือกำลังหารอยู่
   
    elsif count = (WIDTH/2)+1 then
     count <= 0;  -- เริ่มนับ countใหม่
     done_1 <= '1';  -- หารเสร็จเเล้ว
    
    elsif temp_r >= temp_b and done_1 = '0' then
     temp_r <= temp_r-temp_b;
     temp_q <= temp_q((WIDTH/2)-2 downto 0)&'1';  -- shift left
     temp_b <= '0'&temp_b(WIDTH-1 downto 1);  -- shift right
     count <= count + 1;
     
    elsif temp_b >= temp_r and done_1 = '0' then
     temp_q <= temp_q((WIDTH/2)-2 downto 0)&'0';  -- shift left
     temp_b <= '0'&temp_b(WIDTH-1 downto 1);  -- shift right
     count <= count + 1;
    
    elsif done_1 = '1' or (temp_r = conv_std_logic_vector(0,WIDTH)) then
     q <= temp_q;
     r <= temp_r((WIDTH/2)-1 downto 0);
    end if;
   end if;
 end process;
end Behavioral;



การออกแบบวงวรคูณ โดยภาษา VHDL

การออกแบบวงวรคูณ
 cr. http://cpre.kmutnb.ac.th/esl

cr. http://cpre.kmutnb.ac.th/esl








หลักการ เอา ตัวขวาสุดของ a ถ้าเป็น 1 ให้ เอา b มาบวกกับ product ถ้า a ตัวขวาสุดเป็น 0 ก็ไม่ต้องมาบวกกับ product แล้ว shift a ไปทางขา shift b ไปทางซ้าย


Input :
clk สัญญาณสาหรับควบคุมการทางานของของระบบ
a เลขฐาน2ขนาด 16 bit
b เลขฐาน2ขนาด 16 bit
 START ตัวบังคับการทางาน

Output:
P ผลการคูณ
DONE ตัวบอกการทางานการคูณ


                                                              Multiplication CODE VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;


entity mul is
Port (
                 clk : in std_logic;
                  a ,b: in std_logic_vector(15 downto 0);
                 START : in std_logic;
                   P : out std_logic_vector(31 downto 0);
                   DONE : out std_logic);
 end mul;
architecture Behavioral of mul is
signal keepA,keepB : std_logic_vector(31 downto 0) ; --รับค่า a และ b
signal count : integer := 0 ;
signal result : std_logic_vector(31 downto 0) ;
signal product : std_logic_vector(31 downto 0) ;
begin
    process(clk)
          begin
           if (rising_edge(clk) ) then
             if( START = '1' )then
                   keepA <= "0000000000000000"&a; -- ให้keepA รับค่าจาก a
                    keepB <= "0000000000000000"&b; -- ให้keepB รับค่าจาก b
                   DONE <= '0'; -- ให้ done เป้น 0 คือกาลังคูณอยู่
             elsif(keepA (0)='1')then
                    result <= keepB ;
                    product <= result + product;
                    keepA(30 downto 0) <= keepA(31 downto 1);--shift_right
                    keepA(31) <= '0';
                    keepB(31 downto 1) <= keepB(30 downto 0);--shift_left
                    keepB(0) <= '0';
                  count <= count +1 ;
             elsif(keepA (0)='0')then
                      result <= "00000000000000000000000000000000";
                      keepA(30 downto 0) <= keepA(31 downto 1);--shift_right
                      keepA(31) <= '0';
                      keepB(31 downto 1) <=keepB(30 downto 0);--shift_left
                      keepB(0) <= '0';
                      product <= result + product;
              end if;
           end if;
   end process;
P <= product;
end Behavioral;


วันศุกร์ที่ 26 กุมภาพันธ์ พ.ศ. 2559

2complement

2complement การบวกเลขฐานสองถ้าค่าเป็นบวกก็บวกกันปกติ ถ้าเป็นลบเราจะเขียนแบบ 2complement คือการนำเลขฐานสองมา invert หรือกลับค่าจาก 0 เป็น 1 และจาก 1 เป็น 0 แล้วบวกด้วย 1 อีกที  เช่น  6 - 3 = 3
                             0111- 0011   เอา 0011 ไป invert 1100 แล้วบวก 1 = 1101
                             0111(6)+1101(-3) = 0011(3)
cr.http://cpre.kmutnb.ac.th/esl
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity full_add is
  port(
a , b, c_in : in std_logic;
sub : in std_logic;
c_out, p : out std_logic);
end full_add;
architecture data_flow of full_add is
signal keep :std_logic;
begin
process(a,b,c_in)
begin
keep <= b xor sub;
c_out <= ((a xor keep) and c_in) or (a and keep);
p <= (a xor keep) xor c_in;
end process;
end data_flow;








Full adder

Full adder  คือ การบวกเลขฐานสอง
                          0 + 0 = 0
                          1 + 0 = 1
                          0 + 1 = 1
                          1 + 1 =  0 (ทด1)
                                  
                           เช่น 
                                         1 1                         
                                      0 0 1 1
                                                +
                                      1 0 0 1
                                      1 1 0 0    (3 + 9 = 12)

สมการ full adder


ตารางความจริง full adder


cr. https://en.wikipedia.org/wiki/Adder_(electronics)

code full-adder :
library ieee;
use ieee.std_logic_1164.all;
entity full_add is
port(
 a , b, c_in : in std_logic;
 c_out, sum : out std_logic);
end full_add;
architecture data_flow of full_add is
begin
 c_out <= ((a xor b) and c_in) or (a and b); -- ตามสมการfull adder หา carry out
 sum <= (a xor b) xor c_in; -- ตามสมการfull adder หาผลรวม
end data_flow;