/*
Copyright: (C) 2002 Brigham Young University

This program is free software; you can redistribute it and/or modify it
under the terms of the Open Source Software License Agreement for JHDL.

You should have received a copy of the Open Source Software License for
JHDL along with this program.  If you have not, download the License
Agreement at http://www.jhdl.org/license.html or write to:

Brad Hutchings
459 Clyde Bldg
Brigham Young University
Provo, UT 84602


*/


import byucc.jhdl.base.CellInterface;
import byucc.jhdl.base.Node;
import byucc.jhdl.base.Wire;

public class DES extends DESLogic{

  //arrays containing permutation order for key permutation, initial data
  //permutation, and final data permutation
  static int[] key_permute =
  {57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,
   44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,
   5,28,20,12,4};
  static int[] i_permute = 
  {58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,
   48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,
   29,21,13,5,63,55,47,39,31,23,15,7};
  static int[] f_permute = 
  {40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,
   45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,
   50,18,58,26,33,1,41,9,49,17,57,25};

  //ports for this cell
  public static CellInterface[] cell_interface={
    in("encode",1),
    in("data_in",64),
    in("key_in",64),
    out("data_out",64)
  };
  
  //cell constructor
  public DES(Node parent,           //parent of this cell
	     Wire encode,           //encipher/decipher signal (1=encipher)
	     Wire data_in,          //input data
	     Wire key_in,           //input key
	     Wire data_out){        //output data
    
    //call superclass constructor
    super(parent);

    //connect Wires to the ports
    connect("encode",encode);
    connect("data_in",data_in);
    connect("key_in",key_in);
    connect("data_out",data_out);

    //perform initial data permutation
    Wire dataip = permute(i_permute,data_in,"dataip");
   
    //perform key permutation
    Wire keyip = permute(key_permute,key_in,"keyip");

    //create arrays of Wires for input/output of the Rounds
    Wire[] datas = new Wire[16];
    Wire[] keys = new Wire[16];

    //create Wire objects in the arrays
    for (int i=0;i<16;i++){
      datas[i] = wire(64,"datas"+i);
      keys[i] = wire(56,"keys"+i);
    }
    
    //instantiate first DES Round (iteration 0)
    desRound(1,encode,dataip,keyip,datas[0],keys[0]);

    //instantiate DES rounds 1-15
    for (int i=1;i<16;i++)
      desRound(i+1,encode,datas[i-1],keys[i-1],datas[i],keys[i]);

    //perform data swap at end of data round 15
    Wire datasw = concat(datas[15].range(31,0),datas[15].range(63,32),
			 "datasw");
    
    //perform final permutation on data
    Wire datafp = permute(f_permute,datasw,"datafp");
   
    //place ciphered data on output
    buf_o(datafp,data_out);
  }  
}

//written by Jeremy Anderson
