Advertisement
AhmadAbdElHakim

DMA

Dec 14th, 2019
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module processor(mode,databus,address,toaddress,write,read,clk); //hldreq,hldack,count,int
  2.  
  3. inout [31:0] databus;
  4.  
  5. output reg [31:0] address;
  6. output reg [31:0] toaddress;
  7. input wire [1:0] mode;      //able to send and receive data to/from RAM or IO devices directly if it wants to
  8. output reg write;
  9. output reg read;
  10. //input wire int;           //interrupt signal signaling that dma has finished
  11. //input wire hldreq;        //hold request
  12. //output reg hldack;        //hold acknowledge for dma
  13. //output reg [9:0] count;       //number of bytes to be transferred by dma (up to 1024 Byte)
  14. input clk;
  15. reg [31:0] register [0:7];
  16. reg [31:0] Dout;
  17.  
  18. // inout ports are wires, so we can't change its value in a block
  19. // we change the value of Dout, and the databus is assigned to be equal to Dout
  20. assign databus=(write)? Dout:32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
  21.  
  22. initial
  23. begin
  24. register[0]=32'b0;
  25. register[1]=32'b0;
  26. register[7]=32'b00000000000000000000000000001111; //'h0f
  27. end          
  28.  
  29. always @(posedge clk)
  30.  case(mode)
  31.     2'b00: // read 5 words (5*32=160)  from io1 to memory location 5    //assume mode=2'b00 equals hldreq=1
  32.           begin
  33.       //hldack<=1'b1;
  34.           address<=32'b00000000000000000000000000000001;    //assume dma req from i01
  35.           read<=1'b1;
  36.           write<=1'b0;
  37.       //count <= 9'b010100000;              //160 bytes
  38.       toaddress <=32'b00000000000000000000000000000110;
  39.       //processor doing some stuff
  40.       end
  41.  
  42.     2'b01: //read io1
  43.         begin
  44.           address<=32'b00000000000000000000000000000001; // memory location of io1
  45.           read<=1'b1;
  46.           write<=1'b0;
  47.       //hldack<=1'b0;
  48.           register[6]<=databus; //read buffer of io1 and store in reg6 -> reg6 = 07 (line 103 & 120)
  49.          end
  50.      2'b10: //write in io2
  51.         begin
  52.           address<=32'b00000000000000000000000000000010; // memory location of io2
  53.           write<=1'b1;      
  54.           read<=1'b0;  
  55.       //hldack<=1'b0;
  56.           Dout<=register[7]; // make io2 buffer = reg7 = 'h0f
  57.          end    
  58.      2'b11: //read memory address 5
  59.         begin
  60.           address<=32'b00000000000000000000000000000101;        
  61.           read<=1'b1;
  62.           write<=1'b0;
  63.       //hldack<=1'b0;
  64.           register[7]=databus; // -> reg7= 5
  65.          end   
  66.     default:register[0]<=1;
  67.  endcase
  68.  
  69.  
  70. endmodule
  71.  
  72.  
  73. module ram(Address,toaddress,Memread,Memwrite,databus,clk);
  74.  
  75. input[31:0] Address;
  76. input[31:0] toaddress;
  77. input Memread;
  78. input Memwrite;
  79. input clk;
  80.  
  81. inout [31:0] databus;
  82. reg [31:0] Dout;
  83. assign databus=(Memread && Address>3) ? Dout:32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
  84.  
  85. reg[31:0]mem[0:50];
  86.  
  87. initial begin
  88. mem[4]=32'b00000000000000000000000000000100;
  89. mem[5]=32'b00000000000000000000000000000101;
  90. end
  91.  
  92. always@(posedge clk)
  93. begin
  94.     if(Address>3)
  95.     begin
  96.     if(Memread)
  97.     begin
  98.     Dout <= mem[Address];
  99.     end
  100.     if (Memwrite)
  101.     begin
  102.     mem[Address] <= databus;
  103.     end
  104.     end
  105.     else if(toaddress>3)
  106.     begin
  107.     if(Memwrite)
  108.     begin
  109.     Dout <= mem[toaddress];
  110.     end
  111.     if (Memread)
  112.     begin
  113.     mem[toaddress] <= databus;
  114.     end
  115.     end
  116.  
  117. end
  118. endmodule
  119.  
  120.  
  121.  
  122. module io1(address,toaddress,write,read,databus);//, dmareq1, dmaack1); //address=1 (memory location of io1)
  123.  
  124. input [31:0] address;
  125. input[31:0] toaddress;
  126. input write;
  127. input read;
  128. //output reg dmareq1;
  129. //input wire dmaack1;
  130. inout [31:0] databus;
  131. reg [31:0] Dout;
  132.  
  133. assign databus=(read && address==1)? Dout:32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
  134.  
  135. reg [31:0] buffer;
  136. initial begin
  137. buffer=32'b00000000000000000000000000000111; //'h07
  138. end
  139.  
  140. always@(address,toaddress)
  141. begin
  142. if (address==1) //&& mode!=2'b00)
  143. begin
  144. if (write)
  145. begin
  146. buffer<=databus;
  147. end
  148. if (read)
  149. begin
  150. Dout<=buffer;
  151. end
  152. end
  153. else if(toaddress==1)
  154. begin
  155. if (read)
  156. begin
  157. buffer<=databus;
  158. end
  159. if (write)
  160. begin
  161. Dout<=buffer;
  162. end
  163. end
  164. end
  165.  
  166. endmodule
  167.  
  168.  
  169. module io2(address,toaddress,write,read,databus);//, dmareq2, dmaack2); //address=2 (memory location of io2)
  170. input [31:0] address;
  171. input[31:0] toaddress;
  172. input write;
  173. input read;
  174. //output reg dmareq2;
  175. //input wire dmaack2;
  176. inout [31:0] databus;
  177. reg [31:0] Dout;
  178.  
  179. assign databus=(read && address==2)? Dout:32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
  180.  
  181. reg [31:0] buffer;
  182. initial begin
  183. buffer=32'b00000000000000000000000000011111; //'h1F
  184. end
  185.  
  186. always@(address)
  187. begin
  188. if (address==2)// && mode!=2'b00)
  189. begin
  190. if (write)
  191. begin
  192. buffer<=databus;
  193. end
  194. if (read)
  195. begin
  196. Dout<=buffer;
  197. end
  198. end
  199.  
  200. else if(toaddress==2)
  201. begin
  202. if (read)
  203. begin
  204. buffer<=databus;
  205. end
  206. if (write)
  207. begin
  208. Dout<=buffer;
  209. end
  210. end
  211. end
  212. endmodule
  213.  
  214. /*module dma (address, write, read, databus, dmareq, dmaack, hldack, int, count);
  215.  
  216. inout [31:0] databus;
  217.  
  218. input [31:0] address;
  219. input write;
  220. input read;
  221. input [1:0] dmareq;
  222. output reg [1:0] dmaack;
  223. //output reg hldreq;        //hold request for processor
  224. input wire hldack;      //hold acknowledge
  225. output reg int;         //interrupt signal signaling that dma has finished for processor
  226. input [9:0] count;      //number of bytes to be transferred by dma
  227. reg [31:0] Dout;
  228.  
  229. assign databus=()? Dout:32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
  230. initial begin
  231. buffer=32'b00000000000000000000000000011111; //'h1F
  232. end
  233.  
  234. always@(address,dmareq)
  235. begin
  236.     //if(dmareq == 2'b01)//io1 requested the dma
  237.     //begin
  238.         //mode=2'b00; //equals hldreq1=1
  239.         dmaack == 2'b01;
  240.         if (write)
  241.         begin
  242.         buffer<=databus;
  243.         end
  244.         if (read)
  245.         begin
  246.         Dout<=buffer;
  247.         end
  248.     //end
  249.     //if(dmareq == 2'b10)//io2 requested the dma
  250.     //begin
  251.         //mode=2'b00; //equals hldreq1=1
  252.         dmaack == 2'b10;
  253.         if (write)
  254.         begin
  255.         buffer<=databus;
  256.         end
  257.         if (read)
  258.         begin
  259.         Dout<=buffer;
  260.         end
  261.     end
  262. end
  263.  
  264. endmodule
  265.  
  266. */
  267.  
  268. module tbb();
  269. reg clock1;
  270. initial
  271. begin
  272. assign clock1=0;
  273. end
  274. always
  275. begin
  276. #5;
  277. assign clock1=~clock1;
  278. #5;
  279. end
  280. wire write,read;
  281. wire [31:0] databus;
  282. wire [31:0]address;
  283. wire [31:0]toaddress;
  284.  
  285. io1 io(address,toaddress,write,read,databus);
  286. io2 ioo(address,toaddress,write,read,databus);
  287.  
  288. ram ramm(address,toaddress,read,write,databus,clock1);
  289.  
  290. processor pro(2'b00,databus,address,toaddress,write,read,clock1);
  291.  
  292.  
  293. endmodule
  294. /*
  295.      2'b100: // read 5 words from io1 to memory location 10 //assume mode=2'b100 equals hldreq1=
  296.         begin
  297.       hldack<=1'b1;
  298.           address<=32'b00000000000000000000000000000101;        
  299.           read<=1'b1;
  300.           write<=1'b0;
  301.           register[7]=databus; // -> reg7= 5
  302.          end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement