<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.naplespu.com/doc/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mirko</id>
		<title>NaplesPU Documentation - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://www.naplespu.com/doc/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mirko"/>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Special:Contributions/Mirko"/>
		<updated>2026-12-01T10:27:39Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.29.1</generator>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2297</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2297"/>
				<updated>2019-07-25T15:23:55Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===Barrier Instruction===&lt;br /&gt;
NPU supports barrier synchronization between threads within the same core or among different tiles. The programmer needs to know the number of threads that require to synchronize, i.e. &amp;lt;code&amp;gt;NumberOfThreads&amp;lt;/code&amp;gt;. For each synchronization there is an unique barrier ID. The intrinsic __builtin_npu_barrier(BarrierID, NumberOfThreads-1) takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1. Note that the same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some execution, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, on the host side, it is required to initialize barrier 0 with Count=4 and barrier 1 with Count=2.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
  &lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
   int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
   C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
   __builtin_npu_barrier(1,3);   //Synchronization Threads:0,1,2,3.&lt;br /&gt;
   &lt;br /&gt;
  if(threadId&amp;lt;2){&lt;br /&gt;
  //execution thread 0,1&lt;br /&gt;
     D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
     __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
   }&lt;br /&gt;
   if(threadId==0){&lt;br /&gt;
     D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
     __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Intrinsics ==&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| void  barrier (int a, int b) &lt;br /&gt;
| Refer to the description of the barrier instruction. a contains the barrier ID, while b contains the number of threads that should synchronize-1&lt;br /&gt;
| barrier&lt;br /&gt;
|-&lt;br /&gt;
| void flush (int a)&lt;br /&gt;
| Flush a cache line to the main memory. a contains the memory address of the cache line. Explicit integer conversion is required.&lt;br /&gt;
| flush&lt;br /&gt;
|-&lt;br /&gt;
| int createmaskv16i32 (v16i32 a)&lt;br /&gt;
| Convert the vector a that is made of all elements equal to 0 or -1 into a 32-bit mask value that can be written in the mask register. It can be used to compute the bitmask when a vector comparison operation is performed using the common C/C++ relational operators.&lt;br /&gt;
| crt_maskv16&lt;br /&gt;
|-&lt;br /&gt;
| void write_mask_reg(int a)&lt;br /&gt;
| Write a 32-bit  bitmask inside the mask register&lt;br /&gt;
| move&lt;br /&gt;
|-&lt;br /&gt;
| void write_mask_regv16i32 (v16i32 a)&lt;br /&gt;
| Write a 512-bit vector mask inside the mask register.&lt;br /&gt;
| crt_maskv16 + move&lt;br /&gt;
|-&lt;br /&gt;
| int read_mask_reg () &lt;br /&gt;
| Read a 32-bit  bitmask from the mask register.&lt;br /&gt;
| move&lt;br /&gt;
|-&lt;br /&gt;
| void write_control_reg (int a, int b)&lt;br /&gt;
| Write values to the mask register. The integer a contains the ID of the sub-register to access, while the integer b contains the data.&lt;br /&gt;
| write_cr&lt;br /&gt;
|-&lt;br /&gt;
| int read_control_reg (int a)&lt;br /&gt;
| Read values from the mask register. The integer a contains the ID of the sub-register to access.&lt;br /&gt;
| read_cr&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2296</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2296"/>
				<updated>2019-07-25T15:02:34Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===Barrier Instruction===&lt;br /&gt;
NPU supports barrier synchronization between threads within the same core or among different tiles. The programmer needs to know the number of threads that require to synchronize, i.e. &amp;lt;code&amp;gt;NumberOfThreads&amp;lt;/code&amp;gt;. For each synchronization there is an unique barrier ID. The intrinsic __builtin_npu_barrier(BarrierID, NumberOfThreads-1) takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1. Note that the same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some execution, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, on the host side, it is required to initialize barrier 0 with Count=4 and barrier 1 with Count=2.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
  &lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
   int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
   C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
   __builtin_npu_barrier(1,3);   //Synchronization Threads:0,1,2,3.&lt;br /&gt;
   &lt;br /&gt;
  if(threadId&amp;lt;2){&lt;br /&gt;
  //execution thread 0,1&lt;br /&gt;
     D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
     __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
   }&lt;br /&gt;
   if(threadId==0){&lt;br /&gt;
     D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
     __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Intrinsics ==&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| void  barrier (int a, int b) &lt;br /&gt;
| Refer to the description of the barrier instruction. a contains the barrier ID, while b contains the number of threads that should synchronize-1&lt;br /&gt;
| barrier&lt;br /&gt;
|-&lt;br /&gt;
| void flush (int a)&lt;br /&gt;
| Flush a cache line to the main memory. a contains the memory address of the cache line. Explicit integer conversion is required.&lt;br /&gt;
| flush&lt;br /&gt;
|-&lt;br /&gt;
| int createmaskv16i32 (v16i32 a)&lt;br /&gt;
| Convert the vector a that is made of all elements equal to 0 or -1 into a 32-bit mask value that can be written in the mask register. It can be used to compute the bitmask when a vector comparison operation is performed using the common C/C++ relational operators.&lt;br /&gt;
| crt_maskv16&lt;br /&gt;
|-&lt;br /&gt;
| void write_mask_reg(int a)&lt;br /&gt;
| Write a 32-bit  bitmask inside the mask register&lt;br /&gt;
| move&lt;br /&gt;
|-&lt;br /&gt;
| void write_mask_regv16i32 (v16i32 a)&lt;br /&gt;
| Write a 512-bit vector mask inside the mask register.&lt;br /&gt;
| crt_maskv16 + move&lt;br /&gt;
|-&lt;br /&gt;
| int read_mask_reg () &lt;br /&gt;
| Read a 32-bit  bitmask from the mask register.&lt;br /&gt;
| move&lt;br /&gt;
|-&lt;br /&gt;
| void write_control_reg (int a, int b)&lt;br /&gt;
| Write values to the mask register. The integer a contains the ID of the sub-register to access, while the integer b contains the data.&lt;br /&gt;
| write_cr&lt;br /&gt;
|-&lt;br /&gt;
| int read_control_reg (int a)&lt;br /&gt;
| Read values from the mask register. The integer a contains the ID of the sub-register to access.&lt;br /&gt;
| read_cr&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2295</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2295"/>
				<updated>2019-07-25T14:57:15Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===Barrier Instruction===&lt;br /&gt;
NPU supports barrier synchronization between threads within the same core or among different tiles. The programmer needs to know the number of threads that require to synchronize, i.e. &amp;lt;code&amp;gt;NumberOfThreads&amp;lt;/code&amp;gt;. For each synchronization there is an unique barrier ID. The intrinsic __builtin_npu_barrier(BarrierID, NumberOfThreads-1) takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1. Note that the same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some execution, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, on the host side, it is required to initialize barrier 0 with Count=4 and barrier 1 with Count=2.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
  &lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
   int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
   C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
   __builtin_npu_barrier(1,3);   //Synchronization Threads:0,1,2,3.&lt;br /&gt;
   &lt;br /&gt;
  if(threadId&amp;lt;2){&lt;br /&gt;
  //execution thread 0,1&lt;br /&gt;
     D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
     __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
   }&lt;br /&gt;
   if(threadId==0){&lt;br /&gt;
     D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
     __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Intrinsics ==&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2294</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2294"/>
				<updated>2019-07-25T14:46:58Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Barrier Instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===Barrier Instruction===&lt;br /&gt;
NPU supports barrier synchronization between threads within the same core or among different tiles. The programmer needs to know the number of threads that require to synchronize, i.e. &amp;lt;code&amp;gt;NumberOfThreads&amp;lt;/code&amp;gt;. For each synchronization there is an unique barrier ID. The intrinsic __builtin_npu_barrier(BarrierID, NumberOfThreads-1) takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1. Note that the same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some execution, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, on the host side, it is required to initialize barrier 0 with Count=4 and barrier 1 with Count=2.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
  &lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
   int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
   C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
   __builtin_npu_barrier(1,3);   //Synchronization Threads:0,1,2,3.&lt;br /&gt;
   &lt;br /&gt;
  if(threadId&amp;lt;2){&lt;br /&gt;
  //execution thread 0,1&lt;br /&gt;
     D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
     __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
   }&lt;br /&gt;
   if(threadId==0){&lt;br /&gt;
     D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
     __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2293</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2293"/>
				<updated>2019-07-25T14:46:20Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Other Aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===Barrier Instruction===&lt;br /&gt;
NPU supports barrier synchronization between threads. The programmer needs to know the number of threads that require to synchronize, i.e. &amp;lt;code&amp;gt;NumberOfThreads&amp;lt;/code&amp;gt;. For each synchronization there is an unique barrier ID. The intrinsic __builtin_npu_barrier(BarrierID, NumberOfThreads-1) takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1. Note that the same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some execution, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, on the host side, it is required to initialize barrier 0 with Count=4 and barrier 1 with Count=2.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
  &lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
   int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
   C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
   __builtin_npu_barrier(1,3);   //Synchronization Threads:0,1,2,3.&lt;br /&gt;
   &lt;br /&gt;
  if(threadId&amp;lt;2){&lt;br /&gt;
  //execution thread 0,1&lt;br /&gt;
     D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
     __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
   }&lt;br /&gt;
   if(threadId==0){&lt;br /&gt;
     D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
     __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2292</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2292"/>
				<updated>2019-07-25T14:42:21Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a lt b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a lt b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a ge b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a ge b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2291</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2291"/>
				<updated>2019-07-25T14:41:44Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NPU, all vector operations are masked and after the boot all hardware lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look to the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   volatile vec16i32 a;&lt;br /&gt;
   volatile vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
   int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
   do_something();&lt;br /&gt;
   c = c^-1;                                       //generate mask for a&amp;gt;=b&lt;br /&gt;
   __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;gt;=b&lt;br /&gt;
   do_somethingelse();&lt;br /&gt;
   __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In those cases, it is important to prevent compiler optimizations of the involved vectors and variable marking them as volatile. The compiler tends to rearrange operation, and it is good practice to double-check the compiler objdumb in order to check the order of the operation. If volatile does not prevent reordering, you might embed the code in a function or use -O0 as optimization flag.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2290</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2290"/>
				<updated>2019-07-25T14:36:18Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   int c = __builtin_nuplus_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NPU.&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2289</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2289"/>
				<updated>2019-07-25T14:34:54Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Standard LLVM intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16f32 a;&lt;br /&gt;
   ...&lt;br /&gt;
   vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2288</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2288"/>
				<updated>2019-07-25T14:33:27Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Other Aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Standard LLVM intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2287</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2287"/>
				<updated>2019-07-25T14:03:19Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| vec16X __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2286</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2286"/>
				<updated>2019-07-25T13:57:36Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| vec16X  __builtin_convertvector(vec16Y a, vec16X)&lt;br /&gt;
| It is is used to express generic vector type-conversion operations. The input vector and the output vector type must have the same number of elements. X can be equal to i32/f32, while Y can be equal to i32/f32&lt;br /&gt;
| sext or&lt;br /&gt;
itof or&lt;br /&gt;
ftoi&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2285</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2285"/>
				<updated>2019-07-25T13:50:23Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_'w'32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_'w'32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2284</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2284"/>
				<updated>2019-07-25T13:49:56Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Vector intrinsics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16'w'32 a, vec16'w'32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmp'xyz'_i32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_eq (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpeq_i32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_ne (vec16'w'32 a, vec16'w'32 b) &lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
| cmpne_i32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2283</id>
		<title>Programming Model</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Programming_Model&amp;diff=2283"/>
				<updated>2019-07-25T13:27:19Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NaplesPU Other Aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= NaplesPU Programming Model =&lt;br /&gt;
&lt;br /&gt;
==SIMD Support==&lt;br /&gt;
Arithmetic operators (+, -, *, /, %), relational operators (==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=), bitwise operators (&amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;), logical operators (&amp;amp;&amp;amp;, ||, !) and assignment operators (=, +=, -=, *=, /=, %=, &amp;lt;&amp;lt;=, &amp;gt;&amp;gt;=, &amp;amp;=, ^=, |=) can be used with scalar and vector types and produce a scalar or vector signed integer result respectively. In some cases, mixed scalar/vector operations are possible. In such a cases, the scalar is considered as a vector with all elements equal to the scalar value.&lt;br /&gt;
&lt;br /&gt;
For instance, to add two vectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a vector with a scalar:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  int b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a+b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In order to access vector elements, it is possible to use the operator []. For instance&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  // assign some values:&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) a[i]=i;&lt;br /&gt;
  int sum = 0;&lt;br /&gt;
  // calculate sum&lt;br /&gt;
  for (int i=0; i&amp;lt;16; i++) sum += a[i];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vectors can be initialized using curly bracket syntax. For instance, a constant vector:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
   const vec16i32 a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or a non-constant vector&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  int x, y, z;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 a = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x};&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Conversions between vectors with the same number of elements can be performed using the LLVM intrisic __builtin_convertvector. Vector types v16i32, v16u32, v16f32 can be converted between each other. Similarly, vector types v8i64, v8u64, v8f64can be converted between each other. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec16i32 d = __builtin_convertvector(a,vec16i32);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It is also possible to convert floating point vectors with a different number of elements. In such a case, it is required to use the two NPU intrinsics __builtin_npu_v8f64tov16f32 or __builtin_npu_v16f32tov8f64. The first one converts 8 double precision FP elements into 8 single precision FP elements that are placed in the first 8 elements of a v16f32 vector. The second one converts the first 8 single precision FP elements of a v16f32 vector into 8 double precision FP elements. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16f32 a;&lt;br /&gt;
  ...&lt;br /&gt;
  vec8f64 b = __builtin_npu_v16f32tov8f64(a);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Vector comparisons can be done in two different ways. It is possible to use the traditional relational operators that result in another vector type of the same size of the two vectors. For instance:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  vec16i32 c = a &amp;lt; b;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the vector c elements will be equal to 0xFFFFFFFF or 0x00000000 according to the result of the comparison. &lt;br /&gt;
In addition, we also provide vector comparison intrinsics, as follow:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
After executing the above code, the integer c will contain a bitmap that can be directly used to write the mask register if required. Using vector comparison intrinsics is the natural way of performing comparisons in NaplesPU.&lt;br /&gt;
&lt;br /&gt;
Note that, in NaplesPU, all instructions are masked and at the beginning, all lanes are enabled. If you want to handle SIMD control flow, you need to explicitly take care of masking operations so that they are only applied to some elements. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 int main (){&lt;br /&gt;
  vec16i32 a;&lt;br /&gt;
  vec16i32 b;&lt;br /&gt;
  …&lt;br /&gt;
  int c = __builtin_npu_mask_cmpi32_slt (a, b) //generate mask for a&amp;lt;b&lt;br /&gt;
  int rm_old = __builtin_npu_read_mask_reg();  //save mask register&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a&amp;lt;b &lt;br /&gt;
  do_something();&lt;br /&gt;
  c = c^-1;                                       //generate mask for a greater equal b&lt;br /&gt;
  __builtin_npu_write_mask_reg(c);             //write mask register for a greater equal b&lt;br /&gt;
  do_somethingelse();&lt;br /&gt;
  __builtin_npu_write_mask_reg(rm_old);        //restore the old mask&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Thread Parallelism ==&lt;br /&gt;
In a NPU core each thread runs the same code provided by the user, through builtins developers can parallelize it or differentiate flows. Each thread has a private stack, while the memory system is distributed and all threads have the same view of the main memory. User can differentiate threads flow based on the running thread ID, as described in [[Core#Control_Register|control register]] each thread can read its ID from the control register through the &amp;lt;code&amp;gt;__builtin_npu_read_control_reg&amp;lt;/code&amp;gt; accessing to the needed control register (for fetching thread ID use 2, for core ID use 0 or 1):&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In the current release, each tile is equipped with 1 NPU core, hence each core ID overlaps with its tile ID. &lt;br /&gt;
&lt;br /&gt;
===Thread Synchronization===&lt;br /&gt;
NaplesPU supports barrier synchronization among threads. The programmer needs to know the number of threads that require to synchronize, i.e. NumberOfThreads. For each synchronization, there is a unique barrier ID. The NaplesPU intrinsic &amp;lt;code&amp;gt;__builtin_npu_barrier(BarrierID, NumberOfThreads-1)&amp;lt;/code&amp;gt; takes care of the synchronization. It is possible to exploit a maximum number of barriers equal to Bmax, i.e. 4 x number of threads. Barrier IDs range from 0 to Bmax-1.  Note that, same barrier ID cannot be used in different kernels and can be used multiple times inside a kernel only by the same threads or by a subset of them.&lt;br /&gt;
&lt;br /&gt;
In the following example, we have four threads and two barriers. After performing some operations, all threads synchronize on barrier 1. Then, only threads 0 and 1 synchronize on barrier 2. Remember that, in the main code, the user has to provide the total number of synchronizing threads:&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 static vec16i32 C[4];&lt;br /&gt;
 static vec16i32 D[2];&lt;br /&gt;
 const vec16i32 A[4]={{...}    ,{...},{...},    {...}};&lt;br /&gt;
 const vec16i32 B[4]={{...},{...},{...},    {...}};&lt;br /&gt;
 int main(){&lt;br /&gt;
  //execution thread 0,1,2,3&lt;br /&gt;
  int threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
  C[threadId] = A[threadId] + B[threadId];&lt;br /&gt;
  __builtin_npu_barrier(1,3);//Synchronization Threads:0,1,2,3.&lt;br /&gt;
  &lt;br /&gt;
 if(threadId&amp;lt;2){&lt;br /&gt;
 //execution thread 0,1&lt;br /&gt;
    D[threadId]=C[threadId*2]+C[(threadId*2)+1];&lt;br /&gt;
    __builtin_npu_barrier(2,1);//Synchronization Threads:0,1.&lt;br /&gt;
  }&lt;br /&gt;
  if(threadId==0){&lt;br /&gt;
    D[threadId]=D[threadId]+D[threadId+1];&lt;br /&gt;
    __builtin_npu_flush((int)(&amp;amp;D[threadId]));&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== NaplesPU Other Aspects ==&lt;br /&gt;
===Flush instruction===&lt;br /&gt;
The NaplesPU ISA has a flush instruction that is required to avoid data to keep stuck in the cache. It is mandatory to use this instruction in case of output data that is required from the host. Otherwise, the host will read data from the main memory that is not coherent with the cache. The flush instruction takes in input the address of the involved variable and flushes an entire 512-bit cache line. Please remember to cast to integer the address, otherwise you will see the following error: “cannot initialize a parameter of type 'int' with an rvalue of type (YOUR VARIABLE TYPE)*'”. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
  int main (){&lt;br /&gt;
   vec16i32 a;&lt;br /&gt;
   vec16i32 b;&lt;br /&gt;
   …&lt;br /&gt;
   vec16i32 c = a + b;&lt;br /&gt;
   __builtin_npu_flush((int)&amp;amp;c);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please, remember that the flush instruction works for a single 512-bit cache line. As a consequence, multiple flush instructions are required in case of variable types bigger than 512 bits.&lt;br /&gt;
&lt;br /&gt;
===Scratchpad memory===&lt;br /&gt;
In addition to traditional main memory, a NPU core supports a scratchpad memory with a different address space. In order to use the scratchpad memory, the &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; keyword should be used when declaring a variable. Note that just a global variable can be placed in the scratchpad memory. For instance, take a look at the above code:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
 __scratchpad int a;&lt;br /&gt;
 int main (){&lt;br /&gt;
  ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The compiler will recognize the keyword and the integer &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; variable will be placed in the scratchpad using appropriate load/store instructions.&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Misc Intrinsics===&lt;br /&gt;
Main NaplesPU builtins are summarized in the following table:&lt;br /&gt;
&lt;br /&gt;
[[File:NPU_Intr.png]]&lt;br /&gt;
&lt;br /&gt;
===NaplesPU Vector intrinsics===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Intrinsic name (when using these intrinsics you should add “__builtin_npu_” as  prefix )&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Operation&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Corresponding Instruction&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32  makevectori32 (int a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16i32 shufflei32 (vec16i32 a, vec16i32 b) or vec16i32 shufflef32 (vec16f32 a, vec16i32 b)&lt;br /&gt;
| Vector Shuffle - it allows elements of vector a to be copied to the output vector in different positions. The elements in vector b specify, for each corresponding position in the destination register, the indexes of the elements in the source vector register. (see the description of the shuffle instruction)&lt;br /&gt;
| shuffle_i32 or shuffle_f32&lt;br /&gt;
|-&lt;br /&gt;
| int mask_cmp'w'32_'xyz' (vec16u32 a, vec16u32 b)&lt;br /&gt;
| Return an integer bitmask where the i-th bit is equal to one if &lt;br /&gt;
(a[i] comp b[i]) is true. Otherwise, the  i-th bit is equal to zero.&lt;br /&gt;
w = type of compare: i for interger and f for float.&lt;br /&gt;
x = s if signed or u otherwise.&lt;br /&gt;
yz = traditional compare conditions, namely: gt, ge, lt, le &lt;br /&gt;
| cmpxyz_i32&lt;br /&gt;
|-&lt;br /&gt;
| vec16f32 makevectorf32 (float a) &lt;br /&gt;
| Create a vector of 16 elements whose elements are all equal to a&lt;br /&gt;
| move_i32&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Learning by Example ==&lt;br /&gt;
===Matrix Multiplication Multithreaded Example===&lt;br /&gt;
&lt;br /&gt;
The following code shows a multithread version of the matrix multiplication kernel running on a NPU core. The code spreads the output computation among all available threads. Since each output element computation is independent of others, the thread parallelism is achieved dividing the outer cycle of the function. Output matrix row calculations are equally spanned across all cores and further spanned across threads. For each thread, the function first calculates the portion of the output matrix to compute at the core level: &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; the dimension of the matrix and &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; the number of NPU core in the system. &lt;br /&gt;
Then, each thread starts computing the outer loop with &amp;lt;code&amp;gt;start_loop = (core_id * N / CORE_NUMB) + thread_id&amp;lt;/code&amp;gt; the portion of output matrix to compute is multiplied to the running core ID (&amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt;) and added to the running thread ID (&amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt;), and each iteration increments by the number of threads in the core &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 void matrix_mult(const int a[N][N], const int b[N][N], int mult[N][N], int core_id, int thread_id) {&lt;br /&gt;
  int start_loop = (core_id * N / CORE_NUMB) + thread_id;&lt;br /&gt;
  int end_loop = N / CORE_NUMB * (core_id + 1);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = start_loop; i &amp;lt; end_loop; i += THREAD_NUMB){&lt;br /&gt;
    for (int j = 0; j &amp;lt; N; j++)&lt;br /&gt;
      for (int k = 0; k &amp;lt; N; k++)&lt;br /&gt;
        mult[i][j] += a[i][k] * b[k][j];&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Parameters &amp;lt;code&amp;gt;core_id&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;thread_id&amp;lt;/code&amp;gt; are passed by the main function, and fetched from NPU control registers through builtins:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
&lt;br /&gt;
In this way, each thread in each core computes a portion of the output matrix concurrently to the others. &lt;br /&gt;
&lt;br /&gt;
The final result is ready when all thread over the system ended the assigned task, a system synchronization is required and in the main function this is achieved using barrier builtin provided by the programming model:&lt;br /&gt;
 __builtin_npu_barrier(42, CORE_NUMB * THREAD_NUMB - 1);&lt;br /&gt;
The builtin above synchronizes &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB&amp;lt;/code&amp;gt; number of threads (total number of thread running in the system) on the ID 42. When all threads hit the barrier the output matrix is ready, although most of it could be in private L1 caches. The next step is to ''flush'' output lines into the main memory:&lt;br /&gt;
 if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
Generally, flush operations are performed by a thread, in this case the first thread in the first core calls the flush builtin which sends output results still in L1 caches to the main memory.  &lt;br /&gt;
&lt;br /&gt;
The full code for the main function is attached below:&lt;br /&gt;
&lt;br /&gt;
 #define CORE_ID    __builtin_npu_read_control_reg(0)&lt;br /&gt;
 #define THREAD_ID  __builtin_npu_read_control_reg(2)&lt;br /&gt;
 &lt;br /&gt;
 int main(){&lt;br /&gt;
    init_matrix(A);&lt;br /&gt;
    init_matrix(B);&lt;br /&gt;
 &lt;br /&gt;
    matrix_mult(A, B, C, CORE_ID, THREAD_ID);&lt;br /&gt;
    __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
 &lt;br /&gt;
    if (THREAD_ID == 0 &amp;amp;&amp;amp; CORE_ID == 0) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; N*N; i += 64 / sizeof(int)) {&lt;br /&gt;
        __builtin_npu_flush((int) &amp;amp;C[i / N][i % N]);&lt;br /&gt;
      }&lt;br /&gt;
      __builtin_npu_write_control_reg(N*N, 12); // For cosimulation purpose&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
   return (int)&amp;amp;C;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Matrix Multiplication Vectorial Example===&lt;br /&gt;
&lt;br /&gt;
On the other hand, the vectorial version of the matrix multiplication function computes the output matrix in a SIMD multithreading fashion. Both input and output matrices are organized in target specific vector types, becoming vectors of vectors. Such an organization results in a distribution of the N column partial results on the 16 hardware lanes; each thread calculates N partial results every cycle. The size of the matrix must be multiple of 16. &lt;br /&gt;
&lt;br /&gt;
 void kernel_function(vec16i32 *A, vec16i32 *B, vec16i32 *C, int N) {&lt;br /&gt;
    uint32_t coreId =  __builtin_npu_read_control_reg(0);&lt;br /&gt;
    uint32_t threadId = __builtin_npu_read_control_reg(2);&lt;br /&gt;
    uint32_t nT = 2; // number of threads&lt;br /&gt;
    uint32_t nL = 16; // number of lanes&lt;br /&gt;
    uint32_t nC = N/nL; &lt;br /&gt;
    uint32_t ndivnT = N/nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId*ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT*nC;&lt;br /&gt;
 &lt;br /&gt;
    for (uint32_t i = coreId; i &amp;lt; ndivnT*nC; i+=CORE_NUMB){&lt;br /&gt;
        uint32_t col = (tIdndivnT+i)%nC;&lt;br /&gt;
        C[tIdndivnTnC+i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++){&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++){&lt;br /&gt;
                C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col]; &lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Please note, the &amp;lt;code&amp;gt; C[tIdndivnTnC+i] += A[tIdndivnTnC+i-col+j][k] * B[(nC*k)+(j*N)+col] &amp;lt;/code&amp;gt; performs 16 operations on 16 different data at time. The organization of the code and the thread parallelization are equivalent to its scalar version.&lt;br /&gt;
&lt;br /&gt;
=== How to compile a kernel ===&lt;br /&gt;
Currently, the NaplesPU toolchain is released with some example kernels located in the ''npu/software/kernels'' folder. We provide makefiles to compile these kernels for NaplesPU. In case you want to add a new kernel, it is suggested to copy a kernel folder and replace C/CPP files with your own source code. Then, remember to modify the makefile updating the SRCS variable with the current main C/CPP filenames.&lt;br /&gt;
&lt;br /&gt;
= OpenCL support for NaplesPU =&lt;br /&gt;
&lt;br /&gt;
OpenCL defines a platform as a set of computing devices on which the host is connected to. Each device is further divided into several compute units (CUs), each of them defined as a collection of processing elements (PEs).&lt;br /&gt;
Recall that the target platform is architecturally designed around a single core, structured in terms of a set of at most eight hardware threads. Each hardware threads competes with each other to have access to sixteen hardware lanes, to execute both scalar and vector operations on 32-bit wide integer or floating-point operands.&lt;br /&gt;
&lt;br /&gt;
[[File:Plat model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The computing device abstraction is physically mapped on the NaplesPU many-core architecture. The NaplesPU many-core can be configured in terms of the number of cores. Each NPU core maps on the OpenCL Compute Unit. Internally, the NPU core is composed of hardware threads, each of them represents the abstraction of the OpenCL processing element.&lt;br /&gt;
&lt;br /&gt;
==Execution Model Matching==&lt;br /&gt;
From the execution model point of view, OpenCL relies on an N-dimensional index space, where each point represents a kernel instance execution. Since the physical kernel instance execution is done by the hardware threads, the OpenCL work-item is mapped onto an NPU single hardware-thread. Consequently, a work-group is defined as a set of hardware threads, and all work-items in a work-group executes on a single computing unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Execution model_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Memory Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL partitions the memory in four different spaces: &lt;br /&gt;
&lt;br /&gt;
* the global and constant spaces: elements in those spaces are accessible by all work-items in all work-group. &lt;br /&gt;
* the local space: visible only to work-items within a work-group.&lt;br /&gt;
* the private space: visible to only a single work-item. &lt;br /&gt;
&lt;br /&gt;
The target-platform is provided of a DDR memory, that is the device memory in OpenCL nomenclature. Consequently, variables are physically mapped on this memory. The compiler itself, by looking at the address-space qualifier, verifies the OpenCL constraints are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each NPU core is also equipped with a [[Scratchpad unit|Scratchpad Memory]], an on-chip non-coherent memory portion exclusive to each core. This memory is compliant with the OpenCL local memory features&lt;br /&gt;
&lt;br /&gt;
[[File:Memory mod_new.png|700px]]&lt;br /&gt;
&lt;br /&gt;
Finally, each hardware thread within the NPU core has a private stack. This memory section is private to each hardware thread, that is the OpenCL work-item, and cannot be addressed by others. As a result, each stack acts as the OpenCL private memory.&lt;br /&gt;
&lt;br /&gt;
==Programming Model Matching==&lt;br /&gt;
&lt;br /&gt;
OpenCL supports two kinds of programming models, data- and task-parallel. A '''data-parallel''' model requires that each point of the OpenCL index space executes a kernel instance. Since each point represents a work-item and these are mapped on hardware threads, the data-parallel requirements are correctly satisfied. Please note that the implemented model is a relaxed version, without requiring a strictly one-to-one mapping on data.&lt;br /&gt;
&lt;br /&gt;
A '''task-parallel''' programming model requires kernel instances are independently executed in any point of the index space. In this case, each work-item is not constrained to execute the same kernel instance of others. The compiler frontend defines a set of builtins that may be used for this purpose. Moreover, each NPU core is built of 16 hardware lanes, useful to realise a lock-step execution. Consequently, OpenCL support is realised to allow the usage of vector types. As a result, the vector execution is supported for the following data types:&lt;br /&gt;
&lt;br /&gt;
* charn, ucharn, are respectively mapped on vec16i8 and vec16u8, where n=16. Other values of n are not supported.&lt;br /&gt;
* shortn, ushortn, are respectively mapped on vec16i16 and vec16i32, where n=16. Other values of n are not supported.&lt;br /&gt;
* intn, uintn, are respectively mapped on vec16i32 and vec16u32, where n=16. Other values of n are not supported.&lt;br /&gt;
* floatn, mapped on vec16f32, where n=16. Other values of n are not supported.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Runtime Design ==&lt;br /&gt;
OpenCL APIs are a set of functions meant to coordinate and manage devices, those provide support for running applications and monitoring their execution. These APIs also provides a way to retrieve device-related information. &lt;br /&gt;
&lt;br /&gt;
The following Figure depicts the UML Class diagram for the OpenCL Runtime as defined in the OpenCL specification. Grey-filled boxes represent not available features due to the absence of hardware support. &lt;br /&gt;
&lt;br /&gt;
[[File:Uml.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The custom OpenCL runtime relies on two main abstractions:&lt;br /&gt;
* Low-level abstractions, not entirely hardware dependent, provide device-host communication support.&lt;br /&gt;
* High-level abstractions, according to OpenCL APIs, administrate the life cycle of a kernel running onto the device.&lt;br /&gt;
&lt;br /&gt;
== OpenCL Example ==&lt;br /&gt;
&lt;br /&gt;
The following code shows a vectorial matrix multiplication in OpenCL running on the NPU device. &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;opencl_stdlib.h&amp;gt;&lt;br /&gt;
 #define WORK_DIM 4&lt;br /&gt;
 &lt;br /&gt;
 __kernel void kernel_function(__global int16 *A, __global int16 *B, __global int16 *C, int rows, int cols)&lt;br /&gt;
 {&lt;br /&gt;
    __private uint32_t threadId = get_local_id(0); &lt;br /&gt;
 &lt;br /&gt;
    uint32_t nT = WORK_DIM; // number of threads&lt;br /&gt;
    uint32_t nL = 16;       // number of lanes&lt;br /&gt;
 &lt;br /&gt;
    uint32_t N = rows;&lt;br /&gt;
    uint32_t nC = N / nL;&lt;br /&gt;
    uint32_t ndivnT = N / nT;&lt;br /&gt;
    uint32_t tIdndivnT = threadId * ndivnT;&lt;br /&gt;
    uint32_t tIdndivnTnC = tIdndivnT * nC;&lt;br /&gt;
    for (uint32_t i = 0; i &amp;lt; ndivnT * nC; i++)&lt;br /&gt;
    {&lt;br /&gt;
        uint32_t col = (tIdndivnT + i) % nC;&lt;br /&gt;
        C[tIdndivnTnC + i] = 0;&lt;br /&gt;
        for (uint32_t j = 0; j &amp;lt; nC; j++)&lt;br /&gt;
        {&lt;br /&gt;
            for (uint32_t k = 0; k &amp;lt; nL; k++)&lt;br /&gt;
            {&lt;br /&gt;
                C[tIdndivnTnC + i] += A[tIdndivnTnC + i - col + j][k] * B[(nC * k) + (j * N) + col];&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=L2_and_Directory_cache_controller&amp;diff=2273</id>
		<title>L2 and Directory cache controller</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=L2_and_Directory_cache_controller&amp;diff=2273"/>
				<updated>2019-07-12T09:12:20Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Replacement Logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The '''Directory controller''' manages the L2 cache and the ownership of memory lines, it is organized in a distributed directory structure.&lt;br /&gt;
&lt;br /&gt;
== Introduction == &lt;br /&gt;
This component is composed of three stages, each one with particular tasks. This approach has been taken in order to manage the complexity of the component and to ease testing phase.&lt;br /&gt;
&lt;br /&gt;
This component interfaces with the Network Interface in order to send/receive coherence requests.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:L2_cache.jpg|Directory Controller]]&lt;br /&gt;
&lt;br /&gt;
== Stage 1 ==&lt;br /&gt;
Stage 1 is responsible for issuing requests to the control logic. All requests are coherence request/response from the network interface.&lt;br /&gt;
&lt;br /&gt;
=== TSHR Signals ===&lt;br /&gt;
The arbiter checks if a pending request is already issued in the pipeline or ''ongoing'' in the TSHR (see [[L2 and Directory cache controller#TSHR Update Logic | TSHR Update Logic]]). Tags and sets for each type of request are forwarded from TSHR to the arbiter. &lt;br /&gt;
TSHR entries are considered valid for that class of request if and only if its hit signal is asserted:&lt;br /&gt;
 &lt;br /&gt;
 // Signals to TSHR&lt;br /&gt;
 assign ni_request_address                             = ni_request.memory_address;&lt;br /&gt;
 assign dc1_tshr_lookup_tag[REQUEST_TSHR_LOOKUP_PORT]  = ni_request_address.tag;&lt;br /&gt;
 assign dc1_tshr_lookup_set[REQUEST_TSHR_LOOKUP_PORT]  = ni_request_address.index;&lt;br /&gt;
 &lt;br /&gt;
 // Signals from TSHR&lt;br /&gt;
 assign request_tshr_hit                               = tshr_lookup_hit[REQUEST_TSHR_LOOKUP_PORT];&lt;br /&gt;
 assign request_tshr_index                             = tshr_lookup_index[REQUEST_TSHR_LOOKUP_PORT];&lt;br /&gt;
 assign request_tshr_entry_info                        = tshr_lookup_entry_info[REQUEST_TSHR_LOOKUP_PORT];&lt;br /&gt;
&lt;br /&gt;
=== Stall Protocol ROM ===&lt;br /&gt;
In order to be compliant with the coherence protocol all incoming coherence requests on blocks whose coherence state is non-stable state have to be stalled. This task is performed through a protocol ROM whose output signal will stall the issue of that coherence request when asserted, e.g. when a block is in state S_D and a ''GetS'', ''GetM'' or a ''replacement'' request on the same block are stalled. &lt;br /&gt;
In order to assert this signal the protocol ROM receives in input the type of the request, the state and the actual owner of the block:&lt;br /&gt;
&lt;br /&gt;
 assign dpr_state              = tshr_lookup_entry_info[REQUEST_TSHR_LOOKUP_PORT].state;&lt;br /&gt;
 assign dpr_message_type       = ni_request.packet_type;&lt;br /&gt;
 assign dpr_from_owner         = ni_request.source == request_tshr_entry_info.owner;&lt;br /&gt;
 &lt;br /&gt;
 dc_stall_protocol_rom stall_protocol_rom (&lt;br /&gt;
 .input_state         ( dpr_state        ),&lt;br /&gt;
 .input_request       ( dpr_message_type ),&lt;br /&gt;
 .input_is_from_owner ( dpr_from_owner   ),&lt;br /&gt;
 .dpr_output_stall    ( stall_request    )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Note that if the request does not come from the current owner it can be issued because it does not change the coherence state for the block (see [[L2 and Directory cache controller#Protocol ROM | Coherence Protocol]]).&lt;br /&gt;
&lt;br /&gt;
=== Issuing a Request ===&lt;br /&gt;
In order to issue a request, it is required that:&lt;br /&gt;
* TSHR is not full and the address of the request is not already in the TSHR; &lt;br /&gt;
* the network interface is available;&lt;br /&gt;
* further stages are not busy;&lt;br /&gt;
&lt;br /&gt;
The following code shows the issuing logic case for a replacement request, other cases are similar:&lt;br /&gt;
&lt;br /&gt;
 can_issue_replacement_request = !rp_empty &amp;amp;&amp;amp; &lt;br /&gt;
 &lt;br /&gt;
    !tshr_full &amp;amp;&amp;amp; !replacement_request_tshr_hit &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
    ! (( dc2_pending ) || ( dc3_pending )) &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
    ni_forwarded_request_network_available &amp;amp;&amp;amp; ni_response_network_available;&lt;br /&gt;
&lt;br /&gt;
A cache coherence request adds more constraints other than those above, that is:&lt;br /&gt;
* the network interface provides a valid request;&lt;br /&gt;
* if the request is already in TSHR it has to be not valid;&lt;br /&gt;
* if the request is already in TSHR and valid it must not have been stalled by Protocol ROM (see [[L2 and Directory cache controller#Stall Protocol ROM | Stall Signals]]).&lt;br /&gt;
&lt;br /&gt;
The latter two are added in order to give priority to pending requests first.&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_request = ni_request_valid &amp;amp;&amp;amp; &lt;br /&gt;
 &lt;br /&gt;
     !tshr_full &amp;amp;&amp;amp; &lt;br /&gt;
 &lt;br /&gt;
    ( !request_tshr_hit || &lt;br /&gt;
           ( request_tshr_hit  &amp;amp;&amp;amp; !request_tshr_entry_info.valid) ||&lt;br /&gt;
           ( request_tshr_hit &amp;amp;&amp;amp; request_tshr_entry_info.valid  &amp;amp;&amp;amp; !stall_request ) ) &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
    ! (( dc2_pending ) || ( dc3_pending )) &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
    ni_forwarded_request_network_available &amp;amp;&amp;amp; ni_response_network_available;&lt;br /&gt;
&lt;br /&gt;
Finally, responses are never stalled, those are elaborated whenever the network interface outputs a response:&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_response = ni_response_valid;&lt;br /&gt;
&lt;br /&gt;
=== Requests Scheduler ===&lt;br /&gt;
Once the issuing conditions have been verified, two or more requests could be ready to be scheduled at the same time so a fixed-priority scheduler is used. &lt;br /&gt;
In particular this scheduler uses fixed priorities set as below:&lt;br /&gt;
&lt;br /&gt;
# replacement request&lt;br /&gt;
# coherence response &lt;br /&gt;
# coherence request&lt;br /&gt;
&lt;br /&gt;
This ordering ensures coherence is preserved. Once a type of request is scheduled this block drives the output signals for the second stage.&lt;br /&gt;
&lt;br /&gt;
=== L2 Tag &amp;amp; Directory State Cache ===&lt;br /&gt;
Finally, a cache memory stores L2 tags and their directory state (recall that the directory is inclusive). The directory state is updated whenever a request is processed by Stage 3 and the protocol modifies it.&lt;br /&gt;
&lt;br /&gt;
== Stage 2 ==&lt;br /&gt;
&lt;br /&gt;
Stage 2 manages  L2 Data and Info caches, and forwards signals from Stage 1 to Stage 3. It also contains all related logic for managing cache hits and block replacement. The policy used to replace a block is LRU (Least Recently Used). &lt;br /&gt;
&lt;br /&gt;
The L2 cache contains cache data along with coherence information, i.e. the owner and sharers list (the directory state is included in [[L2 and Directory cache controller#L2 Tag &amp;amp; Directory State Cache| L2 Directory State Cache]]).&lt;br /&gt;
&lt;br /&gt;
Stage 3 updates LRU and cache data once the request is processed.&lt;br /&gt;
&lt;br /&gt;
=== TSHR ===&lt;br /&gt;
''Transaction Status Handling Register'' is used to track ongoing coherence transaction on scheduled memory blocks; whenever a memory line is in the TSHR it is in a non-stable state. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A TSHR entry comprises the following information:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Valid &lt;br /&gt;
! Address&lt;br /&gt;
! State&lt;br /&gt;
! Sharers list  &lt;br /&gt;
! Owner&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* Valid: entry is valid&lt;br /&gt;
* Address: entry memory address&lt;br /&gt;
* State: actual coherence state&lt;br /&gt;
* Sharers list: list of sharers for the block (one-hot codified)&lt;br /&gt;
* Owner: block owner&lt;br /&gt;
&lt;br /&gt;
See [[L1_Cache_Controller#Implementation details |MSHR]]  for details about this module implementation.&lt;br /&gt;
&lt;br /&gt;
== Stage 3 ==&lt;br /&gt;
Stage 3 is responsible for the actual execution of requests based on the protocol ROM. Once a request is processed, this module issues signals to the units in the above stages in order to update information and data in caches properly. Every group of signals to a particular unit is managed by a subsystem, each one represented in the picture below. Each subsystem is simply a combinatorial logic that &amp;quot;converts&amp;quot; signals from protocol ROM in proper commands to the relative unit.&lt;br /&gt;
&lt;br /&gt;
[[File:Stage3.png|800px|DC stage 3]]&lt;br /&gt;
&lt;br /&gt;
=== Current State Selector ===&lt;br /&gt;
Before a coherence request is processed the correct source for cache block state has to be chosen. These data can be fetched from:&lt;br /&gt;
&lt;br /&gt;
* cache memory;&lt;br /&gt;
* TSHR;&lt;br /&gt;
* replacement queue;&lt;br /&gt;
&lt;br /&gt;
The following code shows how the control logic selects the information for the issued request:&lt;br /&gt;
&lt;br /&gt;
 always_comb begin&lt;br /&gt;
 	if ( dc2_message_tshr_hit ) begin&lt;br /&gt;
 		current_address      = dc2_message_address;&lt;br /&gt;
 		current_state        = dc2_message_tshr_entry_info.state;&lt;br /&gt;
 		current_sharers_list = dc2_message_tshr_entry_info.sharers_list;&lt;br /&gt;
 		current_owner        = dc2_message_tshr_entry_info.owner;&lt;br /&gt;
 	end else if ( dc2_message_cache_hit ) begin&lt;br /&gt;
 		current_address      = dc2_message_address;&lt;br /&gt;
 		current_state        = dc2_message_cache_state;&lt;br /&gt;
 		current_sharers_list = dc2_message_cache_sharers_list;&lt;br /&gt;
 		current_owner        = dc2_message_cache_owner;&lt;br /&gt;
 	end else if (is_replacement) begin&lt;br /&gt;
 		current_address      = dc2_message_address;&lt;br /&gt;
 		current_state        = dc2_replacement_state;&lt;br /&gt;
 		current_sharers_list = dc2_replacement_sharers_list;&lt;br /&gt;
 		current_owner        = dc2_replacement_owner;&lt;br /&gt;
 	end else begin&lt;br /&gt;
 		current_address      = dc2_message_address;&lt;br /&gt;
 		current_state        = {`DIRECTORY_STATE_WIDTH{1'b0}}; // State N&lt;br /&gt;
 		current_sharers_list = {`TILE_COUNT{1'b0}};&lt;br /&gt;
 		current_owner        = tile_address_t'(TILE_MEMORY_ID);&lt;br /&gt;
 	end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As shown in the above logic, if a TSHR hit occurs then the most updated information for that block are retrieved from the THSR. Otherwise, if a cache hit occurs the information required are fetched from the L2 cache. In case of replacement, those are retrieved from the replacement output signals from the previous stage. If none of the conditions above are met then cache block is considered in state N.&lt;br /&gt;
&lt;br /&gt;
=== Protocol ROM ===&lt;br /&gt;
This module implements the coherence protocol as represented in the figure below. It takes in input the current state and the request type and decodes the next actions.&lt;br /&gt;
&lt;br /&gt;
[[File:MSI_Protocol_dc-rom_new.png|1600px]]&lt;br /&gt;
&lt;br /&gt;
The coherence protocol used is MSI plus some changes due to the directory's inclusivity. In particular, a new stable state has been added, ''N'', meaning the block ''is not'' cached in the directory and has to be fetched from the main memory. The N state has been necessary since when a block reaches the stable state ''I'' states that the block ''is'' cached ''only'' by directory controller, and it is not present in any L1 cache, but the directory still has information on the block. While the directory has no information on blocks in state N.&lt;br /&gt;
&lt;br /&gt;
Furthermore, new non-stable states have been added:&lt;br /&gt;
&lt;br /&gt;
* state '''MN_A''' in which the directory controller is evicting the block which was in state M, and is waiting for an acknowledge message (MC_Ack) from the main memory. This might happens after a replacement request issued for that block. Further requests on the same block are ''stalled'' until data has been received from block owner and sent to the memory. Note that the block is ''invalidated'' so new access to the main memory is necessary;&lt;br /&gt;
* state '''SN_A''' in which the directory controller is evicting the block which was in state S, and is waiting for an acknowledge message (MC_Ack) from the main memory. Similar to the '''MN_A''' state;&lt;br /&gt;
* state '''NS_D''' in which the directory controller is waiting for data coming from the memory. This might occurs after an Fwd-getS request on a block in state N. Further requests on the same block are stalled until data has been received from the main memory and sent to requestor(s).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For further details about the memory coherence protocol, please refer to:&lt;br /&gt;
* ''[[MSI Protocol]]''&lt;br /&gt;
&lt;br /&gt;
=== TSHR Update Logic ===&lt;br /&gt;
TSHR could be updated in three different ways:&lt;br /&gt;
&lt;br /&gt;
* entry allocation;&lt;br /&gt;
* entry deallocation;&lt;br /&gt;
* entry update.&lt;br /&gt;
&lt;br /&gt;
TSHR is used to store cache lines data whose coherence transactions are ''ongoing''. This is the case in which a cache line is in a non-stable state. So an entry allocation is made every time the cache line's state moves towards a non-stable state. In the opposite way, deallocation is performed whenever a cache line's state enters a stable state. Finally, an update is made when there is something to change regarding the TSHR line but the cache line's state is non-stable yet:&lt;br /&gt;
&lt;br /&gt;
 assign tshr_allocate     =  current_state_is_stable &amp;amp; !next_state_is_stable;&lt;br /&gt;
 assign tshr_deallocate   = !current_state_is_stable &amp;amp;  next_state_is_stable; &lt;br /&gt;
 assign tshr_update       = !current_state_is_stable &amp;amp; !next_state_is_stable &amp;amp; coherence_update_info_en;&lt;br /&gt;
&lt;br /&gt;
Note that, if the operation is an entry allocation then the index of the first entry available is passed directly by the TSHR module. Remember that at this point there is surely an empty TSHR line otherwise the request would have not been issued (see [[L2 and Directory cache controller#Issue Signals | Issue Signals]]), since all pending requests are stalled when the TSHR is full.&lt;br /&gt;
&lt;br /&gt;
In case of update or deallocation, the index of the entry is forwarded by Stage 1 (through Stage 2):&lt;br /&gt;
&lt;br /&gt;
 assign dc3_update_tshr_index = tshr_allocate ? tshr_empty_index : dc2_message_tshr_index;&lt;br /&gt;
&lt;br /&gt;
=== Cache Update Logic ===&lt;br /&gt;
Cache could be updated in three different ways:&lt;br /&gt;
&lt;br /&gt;
* entry allocation;&lt;br /&gt;
* entry deallocation;&lt;br /&gt;
* entry update.&lt;br /&gt;
&lt;br /&gt;
Unlike TSHR, the cache stores cache lines data whose coherence transactions are ''completed'', and the tracked ache line is considered in a stable state. So an entry allocation is made every time the cache line's state moves towards a stable state from non-stable and it was not already into the cache. In the opposite way, deallocation whenever a cache line's state enters a non-stable state, then it is tracked in the TSHR (see [[L2 and Directory cache controller#TSHR Update Logic | TSHR Update Logic]]). Finally, an update occurs whenever there is something to change regarding the cache line in compliance with the protocol ROM:&lt;br /&gt;
&lt;br /&gt;
 assign allocate_cache    = next_state_is_stable &amp;amp; ( coherence_update_info_en | dpr_output.store_data ) &amp;amp; ~(tshr_deallocate &amp;amp; dpr_output.invalidate_cache_way) &amp;amp; ~update_cache; &lt;br /&gt;
 assign deallocate_cache  = tshr_allocate &amp;amp; dc2_message_cache_hit ;&lt;br /&gt;
 assign update_cache      = current_state_is_stable &amp;amp; next_state_is_stable &amp;amp; dc2_message_cache_hit &amp;amp; ( coherence_update_info_en | dpr_output.store_data );&lt;br /&gt;
&lt;br /&gt;
=== Replacement Logic ===&lt;br /&gt;
Whenever a replacement request occurs the current cache block is invalidated, but the entry is not freed. That is because the same cache line is replaced with a new valid one from a previous coherence request with the same set. The replaced cache block is queued in a replacement queue until Stage 1 issues it (replacement requests have the maximum priority and will be scheduled as soon as they are pending (see [[L2 and Directory cache controller#Requests Scheduler | Requests Scheduler]])).&lt;br /&gt;
&lt;br /&gt;
This module manages the replacement queue and allows a cache block to be enqueued whenever there is a replacement, this happens whenever the actual coherence request need to store data and info in cache memory, then the current request is not a replacement itself (''!is_replacement'') and a cache miss occurs:&lt;br /&gt;
&lt;br /&gt;
 assign do_replacement  = dc2_message_valid &amp;amp;&amp;amp; dc2_message_cache_valid&lt;br /&gt;
        &amp;amp;&amp;amp; ((allocate_cache || update_cache) &amp;amp;&amp;amp; !deallocate_cache) &lt;br /&gt;
        &amp;amp;&amp;amp; !is_replacement &lt;br /&gt;
        &amp;amp;&amp;amp; !dc2_message_cache_hit;&lt;br /&gt;
 &lt;br /&gt;
 assign dc3_replacement_enqueue = dc2_message_valid &amp;amp;&amp;amp; do_replacement;&lt;br /&gt;
&lt;br /&gt;
Signal &amp;lt;code&amp;gt;dc2_message_cache_valid&amp;lt;/code&amp;gt; states if the selected way stores a valid lane, if so this line has to be recalled if in state '''M''', and pushed back to the main memory. In case of hit in the case, expression &amp;lt;code&amp;gt;!dc2_message_cache_hit&amp;lt;/code&amp;gt;, there is no need of replacement since the control logic is updating an existing line.&lt;br /&gt;
&lt;br /&gt;
=== Message Generator ===&lt;br /&gt;
This module sends forward or response messages to the network interface whenever is required by the protocol ROM:&lt;br /&gt;
&lt;br /&gt;
 	dpr_output.message_response_send,&lt;br /&gt;
        ...&lt;br /&gt;
 	dpr_output.message_forwarded_send,&lt;br /&gt;
&lt;br /&gt;
The above snippet shows the output of the protocol ROM related to the output message to generate. When &amp;lt;code&amp;gt;message_response_send&amp;lt;/code&amp;gt; is asserted the directory sends a response over the network, dually, when &amp;lt;code&amp;gt;message_forwarded_send&amp;lt;/code&amp;gt; is high, a forwarded is generated.&lt;br /&gt;
&lt;br /&gt;
Note that this block manages instruction cache misses as well. In such a case, requests are forwarded directly to memory bypassing the coherence logic.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
[[Coherence]]&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Scratchpad_unit&amp;diff=2270</id>
		<title>Scratchpad unit</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Scratchpad_unit&amp;diff=2270"/>
				<updated>2019-07-08T09:19:38Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Kernel example: matrix multiplication */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU core presents a configurable GPU-like oriented scratchpad memory (SPM) with built-in support for bank remapping. Typically, scratchpad memories are organized in multiple independently-accessible memory banks. Therefore if all memory accesses request data mapped to different banks, they can be handled in parallel (at best, with L memory banks, L different gather/scatter fulfilled in one clock cycle). Bank conflicts occur whenever multiple requests are made for data within the same bank. If N parallel memory accesses request the same bank, the hardware serializes the memory accesses, causing an N-way conflict and an N-times slowdown. The NaplesPU SPM dynamic bank remapping mechanism, based on specific kernel access pattern, minimizes bank conflicts.&lt;br /&gt;
&lt;br /&gt;
==Interface and operation==&lt;br /&gt;
&lt;br /&gt;
===Interface===&lt;br /&gt;
[[File:Fig interface.png|center|thumb|SPM interface]]&lt;br /&gt;
As shown in the figure above, the I/O interface of the SPM has control and data signals. Due to the scattered memory access of the processor, all the data signals, both in input and in output, are vectors and every index identifies a corresponding lane of the core unit. So, SPM has the following data inputs for each lane:&lt;br /&gt;
*&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;: address of the memory location to be accessed;&lt;br /&gt;
*&amp;lt;code&amp;gt;Din&amp;lt;/code&amp;gt;: word containing data to be written (in the case of ''scatter'' operation);&lt;br /&gt;
*&amp;lt;code&amp;gt;BM[0..W-1]&amp;lt;/code&amp;gt;: W-bit-long bitmask to enable/disable the writing of each byte of &amp;lt;code&amp;gt;Din&amp;lt;/code&amp;gt; word;&lt;br /&gt;
*&amp;lt;code&amp;gt;M&amp;lt;/code&amp;gt;: bit asserted if the lane will participate in the next instruction execution.&lt;br /&gt;
As for inputs, the SPM has a single data output for each lane that is:&lt;br /&gt;
*&amp;lt;code&amp;gt;Dout&amp;lt;/code&amp;gt;: data stored at the addresses contained in &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;.&lt;br /&gt;
The &amp;lt;code&amp;gt;store&amp;lt;/code&amp;gt; signal is an input control signal. If ''store'' is high, the requested instruction will be a scatter operation, otherwise it is a gather one. The value of the store signal is the same for all the lanes. Due to the variability of latency, it is necessary to introduce some control signals that allow implementing a handshaking protocol between the control logic of the SIMD core and the SPM control logic. These signals are:&lt;br /&gt;
*&amp;lt;code&amp;gt;start&amp;lt;/code&amp;gt; is an input signal and is asserted by the core control unit to request the execution of an instruction;&lt;br /&gt;
*&amp;lt;code&amp;gt;ready&amp;lt;/code&amp;gt; is an output signal and is asserted by the SPM when can process a new instruction;&lt;br /&gt;
*&amp;lt;code&amp;gt;valid&amp;lt;/code&amp;gt; is an output signal and is asserted by the SPM when the execution of an instruction is finished and its outputs are the final outcome.&lt;br /&gt;
&lt;br /&gt;
===FSM model===&lt;br /&gt;
[[File:spm_fsm.png|center|thumb|SPM behaviour]]&lt;br /&gt;
As said, the SPM takes as input L different addresses to provide support to the scattered memory access (and its multi-banking implementation). It can be regarded as a finite state machine with the following two states:&lt;br /&gt;
*Ready&lt;br /&gt;
:- the SPM is ready to accept a new memory request.&lt;br /&gt;
*Busy&lt;br /&gt;
:- the SPM cannot accept any request as it is still processing the previous one.&lt;br /&gt;
In the ''Ready'' state the &amp;lt;code&amp;gt;ready&amp;lt;/code&amp;gt; signal is high and there are no issuing requests. In the ''Busy'' state all input requests will be ignored. Assuming the loaded instruction relates to conflicting addresses, the SPM goes into the ''Busy'' state and sets the &amp;lt;code&amp;gt;ready&amp;lt;/code&amp;gt; signal as low. At the end of the memory access serialization, the SPM passes into the ''Ready'' state. If the SPM is involved in the execution of another instruction, the &amp;lt;code&amp;gt;ready&amp;lt;/code&amp;gt; signal is low and the input instruction is not yet accepted. If an instruction is free of conflicts, the SPM executes it without passing in the ''Busy'' state. So consecutive instructions without conflicts can be executed without interruptions, just as if the SPM were a ''fully pipelined'' execution unit.&lt;br /&gt;
&lt;br /&gt;
===Customizable features===&lt;br /&gt;
The SPM is totally configurable. In particular it is possible to customize the number of lanes of the SIMD core unit (L), the number of memory banks (B), the number of entries contained in a bank (D) and the number of bytes that make up an entry in a memory bank (W) simply by changing the value of the respective parameters in the SPM configuration file.&lt;br /&gt;
&lt;br /&gt;
 `define SM_PROCESSING_ELEMENTS      16		//L&lt;br /&gt;
 `define SM_ENTRIES                  1024	//D&lt;br /&gt;
 `define SM_MEMORY_BANKS             16		//B&lt;br /&gt;
 `define SM_BYTE_PER_ENTRY           4		//W&lt;br /&gt;
 `define SM_ENTRY_ADDRESS_LEN        $clog2(`SM_ENTRIES) 	//entry address lenght&lt;br /&gt;
 `define SM_MEMORY_BANK_ADDRESS_LEN  $clog2(`SM_MEMORY_BANKS) 	//memory bank address lenght&lt;br /&gt;
 `define SM_BYTE_ADDRESS_LEN         $clog2(`SM_BYTE_PER_ENTRY) 	//byte address lenght&lt;br /&gt;
 `define SM_ADDRESS_LEN              `SM_ENTRY_ADDRESS_LEN + `SM_MEMORY_BANK_ADDRESS_LEN + `SM_BYTE_ADDRESS_LEN 	//address length&lt;br /&gt;
&lt;br /&gt;
The overall size of the SPM is given by &amp;lt;code&amp;gt;B*D*W/8&amp;lt;/code&amp;gt; bit.&lt;br /&gt;
&lt;br /&gt;
==Architecture==&lt;br /&gt;
&lt;br /&gt;
===Overview===&lt;br /&gt;
[[File:SPM_arch_col.png|center|thumb|High-level SPM architectural overview and stages]]&lt;br /&gt;
The scratchpad unit is composed of the scratchpad memory and a FIFO request queue. The unit receives inputs from operand fetch unit and outputs toward the rollback unit and the writeback unit. The FIFO request queue size is customizable. A general request enqueued and forwarded to the SPM is organized as follows:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	logic 	 							is_store;&lt;br /&gt;
 	sm_address_t 	[`SM_PROCESSING_ELEMENTS   - 1 : 0] 		addresses;&lt;br /&gt;
 	sm_data_t 	[`SM_PROCESSING_ELEMENTS      - 1 : 0] 		write_data;&lt;br /&gt;
 	sm_byte_mask_t 	[`SM_PROCESSING_ELEMENTS - 1 : 0] 		byte_mask;&lt;br /&gt;
 	logic 		[`SM_PROCESSING_ELEMENTS          - 1 : 0] 	mask;&lt;br /&gt;
 	logic 		[`SM_PIGGYBACK_DATA_LEN           - 1 : 0] 	piggyback_data;&lt;br /&gt;
 } scratchpad_memory_request_t;&lt;br /&gt;
&lt;br /&gt;
Before the requests are queued, a phase of decoding and alignment checking of the type of operation and bitmask generation based on the opcode (to determine which lane are involved) is performed. Afterwards the control logic checks the alignment of the incoming request. Supported operations are both scalar and vector and on byte, half-word and word-aligned addresses. The alignment is checked by the following coding lines:&lt;br /&gt;
&lt;br /&gt;
 assign is_word_aligned		[lane_idx]	= !( |( effective_addresses[lane_idx][1 : 0] ) );&lt;br /&gt;
 assign is_halfword_aligned	[lane_idx]	= !effective_addresses[lane_idx][0];&lt;br /&gt;
&lt;br /&gt;
Then, the following code determines if the access is misaligned based on the current operation, the required alignment for the memory access and the lane mask:&lt;br /&gt;
&lt;br /&gt;
 assign is_misaligned = ( is_word_op	 &amp;amp;&amp;amp; ( |( ~is_word_aligned	&amp;amp; mask[`HW_LANE - 1 : 0] ) ) ) ||&lt;br /&gt;
 			( is_halfword_op &amp;amp;&amp;amp; ( |( ~is_halfword_aligned	&amp;amp; mask[`HW_LANE - 1 : 0] ) ) );&lt;br /&gt;
&lt;br /&gt;
In case of misaligned access or out-of-memory address, the unit enables the ''Rollback handler'', which launches the corresponding trap.&lt;br /&gt;
&lt;br /&gt;
 always_ff @ ( posedge clk, posedge reset )&lt;br /&gt;
 	if ( reset )&lt;br /&gt;
 		spm_rollback_en &amp;lt;= 1'b0;&lt;br /&gt;
 	else&lt;br /&gt;
 		spm_rollback_en &amp;lt;= instruction_valid &amp;amp; ( is_misaligned | is_out_of_memory ); //Rollback handler enabled&lt;br /&gt;
 &lt;br /&gt;
 always_ff @ ( posedge clk ) begin&lt;br /&gt;
 	spm_rollback_pc        &amp;lt;= `SPM_ADDR_MISALIGN_ISR; //location of the corresponding trap&lt;br /&gt;
 	spm_rollback_thread_id &amp;lt;= opf_inst_scheduled.thread_id;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
If an instruction is aligned, a new &amp;lt;code&amp;gt;scratchpad_memory_request_t&amp;lt;/code&amp;gt; instance is created and filled accordly aligned with the operation type. Then, the new request is enqueued in the FIFO request queue, but only if the instruction is valid and if there is no rollback occurring. When the SPM is in the ''Ready'' state (the &amp;lt;code&amp;gt;ready&amp;lt;/code&amp;gt; signal is high) and the FIFO queue is not empty, the oldest request is dequeued and comes to the SPM. At the end of the SPM execution (when the &amp;lt;code&amp;gt;valid&amp;lt;/code&amp;gt; signal is high), the last phase aims to output the results. In case of conflicting operations, results will be available only at the end of all the serialized requests.&lt;br /&gt;
&lt;br /&gt;
===Stage 1===&lt;br /&gt;
The first stage (in the &amp;lt;span style=&amp;quot;color:green;&amp;quot;&amp;gt;green&amp;lt;/span&amp;gt; box in the figure above) has the task of carrying out conflict detection and serialization of conflicting memory requests. If the SPM is in the ''Ready'' state, it will carry out the incoming requests. If a conflict is detected while the SPM is in the ''Ready'' state, the SPM moves to the ''Busy'' state and serializes conflicting requests. If the SPM is in the ''Busy'' state, it will not accept inputs until the last serialized requests is served.&lt;br /&gt;
&lt;br /&gt;
====Address Remapping Unit====&lt;br /&gt;
The ''Address Remapping Unit'' computes in parallel the bank index and the bank offset for each of the L memory addresses coming from the processor lanes. ''Bank index'' (&amp;lt;code&amp;gt;BI[0..L-1]&amp;lt;/code&amp;gt; in figure above) is the index of the bank to which the address is mapped. ''Bank offset'' (&amp;lt;code&amp;gt;BO[0..L-1]&amp;lt;/code&amp;gt; in figure above) is the displacement of the word into the bank. Furthermore, the Address Remapping Unit behavior can be changed at runtime in order to change the relationship between addresses and banks, allowing the adoption of the mapping strategy that best suits the executed workload. The ''Address Decode Unit'' is the component dedicated to actually performing the decoding process of the mapping strategy. The ''cyclic-mapping'' strategy is the only currently supported and is very easy to implement: it assigns the first word to the first memory bank, the second word to the second memory bank and so on, repeating the assignment process every B times (where B is the number of memory banks) and considering bank indexes &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;B-1&amp;lt;/code&amp;gt; as consecutive. For the ''cyclic-mapping'' strategy, indexes e offsets are obtained simply dividing the address as follows.&lt;br /&gt;
&lt;br /&gt;
 assign bank_offset    = address[`SM_ADDRESS_LEN - 1   -:  `SM_ENTRY_ADDRESS_LEN        ];&lt;br /&gt;
 assign bank_index     = address[`SM_BYTE_ADDRESS_LEN  +:  `SM_MEMORY_BANK_ADDRESS_LEN  ];&lt;br /&gt;
&lt;br /&gt;
====Serialization Logic Unit====&lt;br /&gt;
The ''Serialization Logic Unit'' performs the conflict detection and the serialization of the conflicting requests. It implements an iterative algorithm which defines a set of non-conflicting pending requests to be executed at every clock cycle. Whenever an ''n''-way conflict is detected, the Serialization Logic Unit puts the SPM in the ''Busy'' state and splits the requests into ''n'' conflict-free requests issued serially in the next ''n'' clock cycles, causing an ''n''-times slowdown. When the last request is issued, the Serialization Logic Unit put the SPM in the ''Ready'' state. Notice that for the Serialization Logic Unit, multiple accesses to the same address are not seen as a conflict, as in this occurrence a broadcast mechanism is activated. This broadcast mechanism provides an efficient way to satisfy multiple read requests for the same word.&lt;br /&gt;
&lt;br /&gt;
[[File:SPM serlog1.png|center|thumb|High-level SPM Serialization Logic Unit]]&lt;br /&gt;
&lt;br /&gt;
Among other things, as shown in the figure above, the Serialization Logic Unit takes in input the L-bit-long mask (&amp;lt;code&amp;gt;M&amp;lt;/code&amp;gt; in the figure above) identifying the lanes to serve. At every clock cycle, it keeps track of the requests to be served in that cycle and to postpone to the next cycle by defining three masks of L bits:&lt;br /&gt;
*&amp;lt;code&amp;gt;M'&amp;lt;/code&amp;gt;: pending requests' mask;&lt;br /&gt;
*&amp;lt;code&amp;gt;M&amp;lt;nowiki&amp;gt;''&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;: mask of non-conflicting pending requests to serve in the current clock cycle;&lt;br /&gt;
*&amp;lt;code&amp;gt;M&amp;lt;nowiki&amp;gt;'''&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;: mask of still-conflicting pending requests to postpone.&lt;br /&gt;
&lt;br /&gt;
The Serialization Logic Unit is composed of two main components: the ''Request Issue Unit'' and the ''Address Conflict Logic''.&lt;br /&gt;
&lt;br /&gt;
=====Request Issue Unit=====&lt;br /&gt;
The ''Request Issue Unit'' has the task of storing information about requests throughout the serialization process.&lt;br /&gt;
&lt;br /&gt;
[[File:SPM riu fsm.png|center|thumb|SPM Request Issue Unit finite state machine]]&lt;br /&gt;
&lt;br /&gt;
The Requests Issue Unit keeps the information on a set of requests for as long as it takes to satisfy it, saving the information in a set of internal registers. When the unit is in the ''Ready'' state, it selects the bitmask received from the SPM (&amp;lt;code&amp;gt;M&amp;lt;/code&amp;gt; in the figure above or &amp;lt;code&amp;gt;input_pending_mask&amp;lt;/code&amp;gt; in the snippet below) as pending requests' mask (&amp;lt;code&amp;gt;M'&amp;lt;/code&amp;gt;) and saves the datapath internally. When the unit is in the ''Busy'' state, it rejects input mask from SPM, internal registers are disabled and a multiplexer outputs data previously saved in internal registers. The unit remains in the ''Busy'' state and uses the still-conflicting pending requests' mask (&amp;lt;code&amp;gt;M&amp;lt;nowiki&amp;gt;'''&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;) received from the Address Conflict Logic as pending requests' mask (&amp;lt;code&amp;gt;M'&amp;lt;/code&amp;gt;) until it receives a completely-low bitmask and the serialization process comes to an end. The following code shows the output decoder based on the current state of the unit:&lt;br /&gt;
&lt;br /&gt;
 always_comb begin&lt;br /&gt;
 	case (current_state)&lt;br /&gt;
 		READY :&lt;br /&gt;
 		begin&lt;br /&gt;
 			output_selection        = 1'b0;&lt;br /&gt;
 			input_reg_enable        = 1'b1;&lt;br /&gt;
 			ready                   = 1'b1;&lt;br /&gt;
 			output_is_last_request  = !conflict &amp;amp;&amp;amp; (|input_pending_mask != 0);&lt;br /&gt;
 		end&lt;br /&gt;
 		ISSUING_REQUESTS :&lt;br /&gt;
 		begin&lt;br /&gt;
 			output_selection        = 1'b1;&lt;br /&gt;
 			input_reg_enable        = 1'b0;&lt;br /&gt;
 			ready                   = 1'b0;&lt;br /&gt;
 			output_is_last_request  = !conflict;&lt;br /&gt;
 		end&lt;br /&gt;
 	endcase&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The unit moves to the ''Busy'' state whenever it detects a conflict.&lt;br /&gt;
&lt;br /&gt;
 assign conflict = (|input_still_pending_mask != 0);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;last&amp;lt;/code&amp;gt; signal is asserted when a whole set of requests from an instruction is satisfied in a clock cycle. This signal will be used by the ''Collector Unit'' to validate the collection of the results of a block of serialized requests.&lt;br /&gt;
&lt;br /&gt;
=====Address Conflict Logic=====&lt;br /&gt;
The ''Address Conflict Logic'' analyzes the mask of pending requests and determines a subset of non-conflicting requests that can be satisfied in the current clock cycle. Still-conflicting requests are defined doing a bitwise and between the pending requests' mask and the 1's complement of requests satisfied during the current clock cycle. Then, they are sent back to the ''Request Issue Unit'' according to the following code:&lt;br /&gt;
&lt;br /&gt;
 assign still_pending_mask = ~satisfied_mask &amp;amp; pending_mask;&lt;br /&gt;
&lt;br /&gt;
The unit gives a priority value to each request and selects the highest priority request and the set of pending requests which do not point to the same memory bank as the set that can be executed. The priority value is related to the lane index (requests from lane &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; have the highest priority value and so on) and the evaluation takes up just one clock cycle. The Address Conflict Logic is composed of the following parts:&lt;br /&gt;
*''Conflict Detection'': it makes the conflicting requests' mask by comparing bank indexes of pending requests. If a request is pending and collides with an higher-priority request, the conflicts' mask will set a high bit in the position of the lower-priority lane. It is worth noting that all comparators are in parallel.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	for (i = 0; i &amp;lt; `SM_PROCESSING_ELEMENTS; i++) begin&lt;br /&gt;
 		for (j = i + 1; j &amp;lt; `SM_PROCESSING_ELEMENTS; j++)&lt;br /&gt;
 			assign conflict_matrix[j][i] = ((bank_offsets[i] == bank_offsets[j]) &amp;amp;&amp;amp; pending_mask[i]);&lt;br /&gt;
 	end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
*''Broadcast Selection'': it implements the broadcasting mechanism, useful if several lanes require the read operation on the same word. It elects the address of the higher-priority pending request as broadcast address. Then it compares the bank indexes and the bank offsets of the pending requests with those of the elected broadcast address and sets a high bit in the position of the lane that refers to the same address. Since broadcasting makes sense for read-only instructions, the comparators' outputs are put in an AND gate together with the &amp;lt;code&amp;gt;&amp;amp;not;store&amp;lt;/code&amp;gt; (surely high due to the read operation) to generate the broadcast mask.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	for (i = 0; i &amp;lt; `SM_PROCESSING_ELEMENTS; i++)&lt;br /&gt;
 		assign broadcast_mask[i] = ((bank_indexes[i] == broadcast_bank_index) &amp;amp;&amp;amp; (bank_offsets[i] == broadcast_bank_offset) &lt;br /&gt;
 						&amp;amp;&amp;amp; !is_store);&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
*''Decision Logic'': it determines the mask of the requests to be processed in the current clock cycle, starting from the conflicts' mask and the broadcast mask. A request can be executed immediately only if does not collide with any higher-priority request or if wants to read from the broadcast address. The useful requests are set according to the following code:&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	for (i = 0; i &amp;lt; `SM_PROCESSING_ELEMENTS; i++)&lt;br /&gt;
 		assign satisfied_mask[i] = pending_mask[i] &amp;amp;&amp;amp; (broadcast_mask[i] || !conflicts_mask[i]);&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
===Stage 2===&lt;br /&gt;
The second stage (in the &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;red&amp;lt;/span&amp;gt; box in the figure above) hosts the memory banks. According to the address, incoming requests from the lanes are forwarded to the appropriate memory bank.&lt;br /&gt;
&lt;br /&gt;
====Input Interconnect====&lt;br /&gt;
The ''Input Interconnect'' is an interconnection network that steers source data and/or control signals coming from a lane of the core to the destination bank. Because the Input Interconnect follows the ''Serialization Logic Unit'', it only accepts one request per bank. Its structure is composed of as many Bank Steering Units as there are memory banks.&lt;br /&gt;
&lt;br /&gt;
=====Bank Steering Unit=====&lt;br /&gt;
Each ''Bank Steering Unit'' selects any request that refers to the ''i''-th memory bank among all requests to be processed and forwards it correctly. A set of L comparators firstly checks if the bank indexes and the preloaded index ''i'' are equal. Then the results are forwarded to an AND gate together with the mask of the requests to be processed in the current clock cycle. In the case of read operations, due to the broadcasting mechanism, the bank offsets of incoming requests are all the same, so requests are equivalent and it is possible to forward any of them to the memory bank. The bitmask of pending requests of the ''i''-th memory bank are sent to the ''Priority Encoder'' that chooses the index of the highest-priority pending request. To avoid performing incorrect operations, if there is no request for the selected memory bank, the selected index is put in an AND gate with the validity output of the Priority Encoder. Therefore, this output is high only if there is actually a request to be processed for the ''i''-th memory bank.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	for (i = 0; i &amp;lt; `SM_PROCESSING_ELEMENTS; i++)&lt;br /&gt;
 		assign compare_mask[i] = input_bank_indexes[i] == BANK_ADDRESS;&lt;br /&gt;
 endgenerate&lt;br /&gt;
 &lt;br /&gt;
 priority_encoder_npu #(&lt;br /&gt;
 	.INPUT_WIDTH(`SM_PROCESSING_ELEMENTS),&lt;br /&gt;
 	.MAX_PRIORITY(&amp;quot;LSB&amp;quot;)&lt;br /&gt;
 ) controller (&lt;br /&gt;
 	.decode(input_mask &amp;amp; compare_mask), //input_mask is satisfied_mask from Input Interconnect&lt;br /&gt;
 	.encode(selected_address),&lt;br /&gt;
 	.valid(priority_encoder_valid)&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 assign output_enable = input_mask[selected_address] &amp;amp; priority_encoder_valid;&lt;br /&gt;
&lt;br /&gt;
====Banked memory====&lt;br /&gt;
The storage unit is partitioned into B memory banks. Each memory bank receives the bank offset, the source data, and the control signal form the lane that addressed it. It is a ''read-first'' SRAM module: if a write operation occurs, the memory bank firstly will output the content located at the selected address and then it will overwrite it. Each memory bank has a single read/write port with a byte-level write enable signal to support instructions with operand sizes smaller than word (&amp;lt;code&amp;gt;WE&amp;lt;/code&amp;gt; in the figure below). Furthermore, each lane controls a bit in an L-bit mask bus that is propagated through the ''Input Interconnect'' to the appropriate bank and this bit acts as a memory bank enable signal (&amp;lt;code&amp;gt;En&amp;lt;/code&amp;gt; in the figure below).&lt;br /&gt;
&lt;br /&gt;
[[File:SPM mb.png|center|thumb|SPM memory bank interface]]&lt;br /&gt;
&lt;br /&gt;
===Stage 3===&lt;br /&gt;
The third and final stage (in the &amp;lt;span style=&amp;quot;color:yellow;&amp;quot;&amp;gt;yellow&amp;lt;/span&amp;gt; box in the figure above) propagates the data output from the memory banks to the lanes that request it storing the result in the corresponding register. If any conflicting requests have been serialized and the &amp;lt;code&amp;gt;last&amp;lt;/code&amp;gt; signal coming in input from the ''Serialization Logic'' is high, results will be validated (setting the &amp;lt;code&amp;gt;valid&amp;lt;/code&amp;gt; output of the SPM as high) and sent out as a single vector. Due to the memory banks' latency, the non-conflicting requests' mask and the &amp;lt;code&amp;gt;last&amp;lt;/code&amp;gt; signal are delayed to make sure that they come alongside the data linked to the corresponding request.&lt;br /&gt;
&lt;br /&gt;
====Output Interconnect====&lt;br /&gt;
The ''Output Interconnect'' propagates data to the lane that requested it. As shown in the figure below, it is composed of a set of L multiplexer which interconnects all the memory banks with all lanes. Each multiplexer selects one among memory banks' outputs based on the value of the bank index received from the ''Serialization Logic'' and sends it toward the lane from which were addressed. If several lanes require the same data, the requests' bank indexes will be the same and therefore multiplexers will forward the same data to different lanes (according to the broadcasting).&lt;br /&gt;
&lt;br /&gt;
[[File:SPM oi.png|center|thumb|SPM Output Interconnection]]&lt;br /&gt;
&lt;br /&gt;
====Collector Unit====&lt;br /&gt;
At the end of the pipe there is a ''Collector Unit'' which is a set of L registers that collect the results coming from the serialized requests outputting them as a single vector. It saves the results received from the ''Output Interconnection'' in a series of L registers. Each register is enabled by the corresponding bit of the mask of non-conflicting requests to be satisfied in the current clock cycle outgoing from the ''Serialization Logic''. When a request is served successfully, its result is stored in the paired register as shown in the following code.&lt;br /&gt;
&lt;br /&gt;
 genvar lane_idx;&lt;br /&gt;
 generate&lt;br /&gt;
 	for (lane_idx = 0; lane_idx &amp;lt; `SM_PROCESSING_ELEMENTS; lane_idx++) begin&lt;br /&gt;
 		always_ff @(posedge clock, negedge resetn) begin&lt;br /&gt;
 			if (!resetn) begin&lt;br /&gt;
 				//reset&lt;br /&gt;
 			end else begin&lt;br /&gt;
 				if (sm2_satisfied_mask[lane_idx])&lt;br /&gt;
 					sm3_read_data[lane_idx] &amp;lt;= ii_output_data[lane_idx];&lt;br /&gt;
 				end&lt;br /&gt;
 			end&lt;br /&gt;
 		end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
==Usage example==&lt;br /&gt;
As said above, usage of scratchpad memory must be explicitly defined by programmers.&lt;br /&gt;
&lt;br /&gt;
'''To specify that a variable must be saved in the SPM, the global scope and the''' &amp;lt;code&amp;gt;__scratchpad&amp;lt;/code&amp;gt; '''attribute in the declaration line of the variable are mandatory'''.&lt;br /&gt;
&lt;br /&gt;
===Assembly output===&lt;br /&gt;
So, we can declare a variable using the SPM attibute as follows:&lt;br /&gt;
&lt;br /&gt;
 __scratchpad int test_counter = 0;&lt;br /&gt;
&lt;br /&gt;
Then, the NaplesPU compiler saves it in the corresponding memory area during the execution startup rutines (&amp;lt;code&amp;gt;crt0&amp;lt;/code&amp;gt;). Before the execution of the main function, a portion of available memory address space equal to the SPM size is dynamically instantiated and data and variables defined with the SPM attribute are mapped into. In the object file, the SPM initialization is identified by &amp;lt;code&amp;gt;preload_scratchpad&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;preload_scratchpad_vector&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;preload_scratchpad_words&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;preload_scratchpad_remain_bytes&amp;lt;/code&amp;gt; labels.&lt;br /&gt;
&lt;br /&gt;
Analyzing the object file made by the compiler, it is easy to recognize parts of SPM-based instructions in the source code.&lt;br /&gt;
&lt;br /&gt;
For instance, the following C++ code line&lt;br /&gt;
&lt;br /&gt;
 test_counter += 10;&lt;br /&gt;
&lt;br /&gt;
is translated after compilation as the following assembly lines, according to the ISA.&lt;br /&gt;
&lt;br /&gt;
 5e8:	02 00 04 82 	load32_scratchpad s1, (s0)&lt;br /&gt;
 5ec:	50 10 04 44 	addi s1, s1, 10&lt;br /&gt;
 5f0:	02 00 04 a2 	store32_scratchpad s1, (s0)&lt;br /&gt;
&lt;br /&gt;
===Kernel example: matrix multiplication===&lt;br /&gt;
The use of SPM is illustrated through the example of a matrix multiplication &amp;lt;code&amp;gt;C = A*B&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; of NxN dimensions.&lt;br /&gt;
&lt;br /&gt;
In the header file, macros and global variables are defined as follows:&lt;br /&gt;
&lt;br /&gt;
  #define TILE_DIM THREAD_NUMB&lt;br /&gt;
  __scratchpad int As[TILE_DIM][N];&lt;br /&gt;
  __scratchpad int Bs[N][TILE_DIM];&lt;br /&gt;
&lt;br /&gt;
Moreover, the following code is an example of ''tiled'' matrix multiplication kernel.&lt;br /&gt;
&lt;br /&gt;
  void matrix_mult_scratch (int C[N][N], const int A[N][N], const int B[N][N]) {&lt;br /&gt;
    for (unsigned turn = 0; turn &amp;lt; N / TILE_DIM; turn++) {&lt;br /&gt;
      unsigned int col0 = turn * TILE_DIM; //starting column for the current tile&lt;br /&gt;
      unsigned int tid = THREAD_ID;&lt;br /&gt;
      unsigned int row = CORE_ID * TILE_DIM + tid; //row index assigned to the given thread&lt;br /&gt;
      &lt;br /&gt;
      // NxTILE_DIM sub-matrix of B from global to scratchpad memory&lt;br /&gt;
      if (THREAD_ID == 0)&lt;br /&gt;
        for (unsigned int i = 0; i &amp;lt; N; i++)&lt;br /&gt;
          for (unsigned int j = 0; j &amp;lt; TILE_DIM; j++)&lt;br /&gt;
            Bs[i][j] = B[i][col0 + j];&lt;br /&gt;
      &lt;br /&gt;
      __builtin_npu_barrier(CORE_ID + 1, THREAD_NUMB - 1);&lt;br /&gt;
      &lt;br /&gt;
      // row of input matrix A from global to scratchpad memory&lt;br /&gt;
      for (unsigned int i = 0; i &amp;lt; N; i++)&lt;br /&gt;
        As[tid][i] = A[row][i];&lt;br /&gt;
      &lt;br /&gt;
      int sum = 0;&lt;br /&gt;
      for (unsigned int col = 0; col &amp;lt; TILE_DIM; col++) {&lt;br /&gt;
        sum = 0; &lt;br /&gt;
        &lt;br /&gt;
        // evaluation of single element of C matrix&lt;br /&gt;
        for (unsigned int i = 0; i &amp;lt; N; i++)&lt;br /&gt;
          sum += As[tid][i] * Bs[i][col];&lt;br /&gt;
        &lt;br /&gt;
        // last evaluated element of C from scratchpad to global memory&lt;br /&gt;
        C[row][col + col0] = sum;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The matrix multiplication is divided into &amp;lt;code&amp;gt;N/TILE_DIM&amp;lt;/code&amp;gt; iterations. Within each iteration of the outer ''for'' loop, every thread computes&lt;br /&gt;
&amp;lt;code&amp;gt;TILE_DIM&amp;lt;/code&amp;gt; elements of the output matrix &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; and every core evaluates the multiplication between a &amp;lt;code&amp;gt;TILE_DIMxN&amp;lt;/code&amp;gt; sub-matrix of &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;NxTILE_DIM&amp;lt;/code&amp;gt; sub-matrix of &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. The &amp;lt;code&amp;gt;As&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bs&amp;lt;/code&amp;gt; sub-matrices are stored into the local scratchpad memory, saving memory transactions since data from main memory are fetched only once per iteration. To keep the kernel simple, &amp;lt;code&amp;gt;TILE_DIM&amp;lt;/code&amp;gt; must be equal to &amp;lt;code&amp;gt;N / CORE_NUMB&amp;lt;/code&amp;gt; (where &amp;lt;code&amp;gt;CORE_NUMB&amp;lt;/code&amp;gt; is the number of running cores). Furthermore, the total number of running cores and threads must be equal to the following formula: &amp;lt;code&amp;gt;CORE_NUMB * THREAD_NUMB = N&amp;lt;/code&amp;gt; (where &amp;lt;code&amp;gt;THREAD_NUMB&amp;lt;/code&amp;gt; is the number of running threads per core).&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=MC_System&amp;diff=2268</id>
		<title>MC System</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=MC_System&amp;diff=2268"/>
				<updated>2019-07-02T12:55:08Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* MC tile */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
The many-core version of the project provides different type of tiles, although all share the same network- and coherence-related components. On the networking side, a generic tile of the system features a '''Network Interface''' module and a hardware '''Router''', both described in [[Network|Network architecture]] section.&lt;br /&gt;
&lt;br /&gt;
The NaplesPU many-core features a shared-memory system, each NPU core has a private L1 data cache, while the L2 cache is spread all over the instantiated tile along with a '''Directory Controller''' module which handles and stores coherence information for cached memory lines. Both are further described in [[Coherence|Coherence architecture]] section.&lt;br /&gt;
&lt;br /&gt;
== NPU tile ==&lt;br /&gt;
The NPU tile mainly equipped with a NaplesPU GPGPU core, described in [[Core|NaplesPU GPGPU core]] section, and components that interface the core with both networking and coherence systems. The following figure depicts a block view of the NPU tile: &lt;br /&gt;
&lt;br /&gt;
[[File:Tile npu.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The tile implements a '''Cache controller''', called &amp;lt;code&amp;gt;l1d_cache&amp;lt;/code&amp;gt;, which interfaces the core to the memory system. The Cache controller exploits three Network interface virtual channels dedicated to the coherence system (ID 0 to 2), providing an abstracted view of the coherence to the core. For further details of the module and of the coherence system refer to [[Coherence|Coherence architecture]] section. Two channels are also shared with the Directory controller.&lt;br /&gt;
&lt;br /&gt;
Host communication, synchronization system and IO request leverage on the last Virtual Channel (ID 3) named Service Virtual Channel. The &amp;lt;code&amp;gt;tile_npu&amp;lt;/code&amp;gt; module implements an arbiter which dispatches the incoming message from the service channel to the right module, based on the ''message_type'' field. E.g., a message from the host is marked as HOST and is forwarded to the boot manager module in the tile: &lt;br /&gt;
&lt;br /&gt;
 always @( ni_n2c_mes_service_valid, ni_n2c_mes_service, n2c_sync_message, bm_c2n_mes_service_consumed, sc_account_consumed, n2c_mes_service_consumed, io_intf_message_consumed ) begin&lt;br /&gt;
  ...&lt;br /&gt;
  if (ni_n2c_mes_service_valid) begin&lt;br /&gt;
    if( ni_n2c_mes_service.message_type == HOST ) begin&lt;br /&gt;
      bm_n2c_mes_service.data  = ni_n2c_mes_service.data;&lt;br /&gt;
      bm_n2c_mes_service_valid = ni_n2c_mes_service_valid;&lt;br /&gt;
      c2n_mes_service_consumed = bm_c2n_mes_service_consumed;&lt;br /&gt;
    end else if ( ni_n2c_mes_service.message_type == SYNC ) begin&lt;br /&gt;
       if ( n2c_sync_message.sync_type == ACCOUNT ) begin&lt;br /&gt;
         ni_account_mess          = n2c_sync_message.sync_mess.account_mess;&lt;br /&gt;
         ni_account_mess_valid    = ni_n2c_mes_service_valid;&lt;br /&gt;
         c2n_mes_service_consumed = sc_account_consumed;&lt;br /&gt;
    end else if ( n2c_sync_message.sync_type == RELEASE ) begin&lt;br /&gt;
         n2c_release_message      = n2c_sync_message.sync_mess.release_mess;&lt;br /&gt;
         n2c_release_valid        = ni_n2c_mes_service_valid;&lt;br /&gt;
         c2n_mes_service_consumed = n2c_mes_service_consumed;&lt;br /&gt;
    end&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
In case of SYNCH message, the above arbiter checks if the message is an ACCOUNT or a RELEASE type. In the first case, the service message is dispatched to the Synchronization core, otherwise to the NPU core on the service interface, which is directly connected to the Barrier core module allocated in the core.&lt;br /&gt;
&lt;br /&gt;
Possible incoming host requests are defined in the &amp;lt;code&amp;gt;npu_message_service_define.sv&amp;lt;/code&amp;gt; header file:&lt;br /&gt;
&lt;br /&gt;
 typedef enum logic [`MESSAGE_TYPE_LENGTH - 1 : 0] {&lt;br /&gt;
  HOST  = 0,&lt;br /&gt;
  SYNC  = 1,&lt;br /&gt;
  IO_OP = 2&lt;br /&gt;
 } service_message_type_t;&lt;br /&gt;
&lt;br /&gt;
While '''Synchronization Core''' is widely described in the [[Synchronization|Synchronization architecture]] section, in the remains of this section we will focus on the other modules that rely on the Service Virtual Channel.&lt;br /&gt;
&lt;br /&gt;
===C2N Service Scheduler===&lt;br /&gt;
This module stores and serializes requests from modules that attempt to send a message over the Service Virtual Channel. Since this channel has a single entry point, the &amp;lt;code&amp;gt;c2n_service_scheduler&amp;lt;/code&amp;gt; provides a dedicated port to each of the requiring modules and forwards their request to the service virtual channel. When a conflict occurs, this module serializes the conflicting requests, each request is selected by an internal round-robin arbiter. &lt;br /&gt;
&lt;br /&gt;
===Boot Manager===&lt;br /&gt;
Host commands are dispatched to the &amp;lt;code&amp;gt;boot_manager&amp;lt;/code&amp;gt; module interconnected with the Thread Controller and control registers on the core. This module implements core-side logic for the item interface, described in the [[MC_Item|Item Interface]] section, interacting with the core on the basis of messages from the host.&lt;br /&gt;
&lt;br /&gt;
===IO Interface===&lt;br /&gt;
The &amp;lt;code&amp;gt;io_interface&amp;lt;/code&amp;gt; module fetches memory request on the non-coherent memory region and dispatches them to the right IO device allocated within the system. This module mainly converts a memory request into an IO message that will flow over the network. For each memory request from the core, the &amp;lt;code&amp;gt;io_interface&amp;lt;/code&amp;gt; module encapsulates essential information, such as requesting address, in an IO message as follow: &lt;br /&gt;
&lt;br /&gt;
 assign io_intf_available_to_core = ~req_almost_full,&lt;br /&gt;
    req_value_i.io_source     = tile_id_t'(TILE_ID),    &lt;br /&gt;
    req_value_i.io_thread     = ldst_io_thread,&lt;br /&gt;
    req_value_i.io_operation  = io_operation_t'(ldst_io_operation),&lt;br /&gt;
    req_value_i.io_address    = ldst_io_address,&lt;br /&gt;
    req_value_i.io_data       = ldst_io_data;&lt;br /&gt;
     &lt;br /&gt;
IO messages from such a component are always forwarded to the H2C tile:&lt;br /&gt;
&lt;br /&gt;
 assign dest_tile_idx = issue_req ? `TILE_H2C_ID : saved_mess.io_source;&lt;br /&gt;
&lt;br /&gt;
== MC tile ==&lt;br /&gt;
&lt;br /&gt;
[[File:Tile mc.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The MC tile interfaces the system with main memory, which is defined as follow:&lt;br /&gt;
 // To Memory NI&lt;br /&gt;
 output logic  [MANGO_ADDRESS_WIDTH - 1 : 0]             n2m_request_address,&lt;br /&gt;
 output logic                       [63 : 0]             n2m_request_dirty_mask,&lt;br /&gt;
 output logic  [MANGO_DATA_WIDTH - 1 : 0]                n2m_request_data,&lt;br /&gt;
 output logic                                            n2m_request_read,&lt;br /&gt;
 output logic                                            n2m_request_write,&lt;br /&gt;
 output logic                                            n2m_request_is_instr,&lt;br /&gt;
 output logic                                            n2m_avail,&lt;br /&gt;
 &lt;br /&gt;
 // From Memory NI&lt;br /&gt;
 input  logic                                            m2n_request_read_available,&lt;br /&gt;
 input  logic                                            m2n_request_write_available,&lt;br /&gt;
 input  logic                                            m2n_response_valid,&lt;br /&gt;
 input  logic  [MANGO_ADDRESS_WIDTH - 1 : 0]             m2n_response_address,&lt;br /&gt;
 input  logic  [MANGO_DATA_WIDTH - 1 : 0]                m2n_response_data&lt;br /&gt;
&lt;br /&gt;
The memory interface has a simple valid/available handshake and allows read/write operations.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;npu2memory&amp;lt;/code&amp;gt; module turns coherence messages from the NPU core into valid memory requests for the interface above:&lt;br /&gt;
&lt;br /&gt;
 assign request_is_valid                = |request_oh &amp;amp; enable &amp;amp; ~pending_read;&lt;br /&gt;
 assign request_is_read                 = request_is_valid &amp;amp; ( pending_fwd_request_out.packet_type == FWD_GETS | pending_fwd_request_out.packet_type == FWD_GETM ) &amp;amp; grant_oh[1];&lt;br /&gt;
 assign request_is_write                = m2n_request_write_available &amp;amp; ni_response_network_available &amp;amp; ~pending_resp_fifo &amp;amp; request_is_valid &amp;amp; pending_response_out.packet_type == WB &amp;amp; grant_oh[0];&lt;br /&gt;
&lt;br /&gt;
This module also provides endianness swapping supported if required:  &lt;br /&gt;
 if (SWAPEND) begin&lt;br /&gt;
  for ( swap_word = 0; swap_word &amp;lt; 16; swap_word++ ) begin : swap_word_gen&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 +: 8]      = m2n_response_data[swap_word * 32 + 24 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 8 +: 8]  = m2n_response_data[swap_word * 32 + 16 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 16 +: 8] = m2n_response_data[swap_word * 32 + 8 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 24 +: 8] = m2n_response_data[swap_word * 32 +: 8];&lt;br /&gt;
 &lt;br /&gt;
     assign out_data_swap[swap_word * 32 +: 8]               = pending_response_out.data[swap_word * 32 + 24 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 8 +: 8]           = pending_response_out.data[swap_word * 32 + 16 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 16 +: 8]          = pending_response_out.data[swap_word * 32 + 8 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 24 +: 8]          = pending_response_out.data[swap_word * 32 +: 8];&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Parameter ''SWAPEND'' determines whether the swapping logic is allocated or not.&lt;br /&gt;
&lt;br /&gt;
== H2C tile ==&lt;br /&gt;
&lt;br /&gt;
[[File:Tile h2c.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The H2C tile interfaces the system with the host through the Item interface. The &amp;lt;code&amp;gt;npu_item_iternface&amp;lt;/code&amp;gt; interprets incoming item from the host and builds service packets marked as HOST type which encapsulates the command sent by the host. One the packet is ready, the &amp;lt;code&amp;gt;npu_item_interface&amp;lt;/code&amp;gt; forwards it to the destination tile through the service network. Section [[MC_Item|Item Interface]] details service packets sent by this module.&lt;br /&gt;
&lt;br /&gt;
== NONE tile ==&lt;br /&gt;
Empty tile, a NONE tile allocates only shared network- and coherence-related components. &lt;br /&gt;
 &lt;br /&gt;
== HT tile ==&lt;br /&gt;
Described in [[Heterogeneous Tile|Heterogeneous Tile]] section.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=MC_System&amp;diff=2267</id>
		<title>MC System</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=MC_System&amp;diff=2267"/>
				<updated>2019-07-02T12:12:17Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* NPU tile */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
The many-core version of the project provides different type of tiles, although all share the same network- and coherence-related components. On the networking side, a generic tile of the system features a '''Network Interface''' module and a hardware '''Router''', both described in [[Network|Network architecture]] section.&lt;br /&gt;
&lt;br /&gt;
The NaplesPU many-core features a shared-memory system, each NPU core has a private L1 data cache, while the L2 cache is spread all over the instantiated tile along with a '''Directory Controller''' module which handles and stores coherence information for cached memory lines. Both are further described in [[Coherence|Coherence architecture]] section.&lt;br /&gt;
&lt;br /&gt;
== NPU tile ==&lt;br /&gt;
The NPU tile mainly equipped with a NaplesPU GPGPU core, described in [[Core|NaplesPU GPGPU core]] section, and components that interface the core with both networking and coherence systems. The following figure depicts a block view of the NPU tile: &lt;br /&gt;
&lt;br /&gt;
[[File:Tile npu.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The tile implements a '''Cache controller''', called &amp;lt;code&amp;gt;l1d_cache&amp;lt;/code&amp;gt;, which interfaces the core to the memory system. The Cache controller exploits three Network interface virtual channels dedicated to the coherence system (ID 0 to 2), providing an abstracted view of the coherence to the core. For further details of the module and of the coherence system refer to [[Coherence|Coherence architecture]] section. Two channels are also shared with the Directory controller.&lt;br /&gt;
&lt;br /&gt;
Host communication, synchronization system and IO request leverage on the last Virtual Channel (ID 3) named Service Virtual Channel. The &amp;lt;code&amp;gt;tile_npu&amp;lt;/code&amp;gt; module implements an arbiter which dispatches the incoming message from the service channel to the right module, based on the ''message_type'' field. E.g., a message from the host is marked as HOST and is forwarded to the boot manager module in the tile: &lt;br /&gt;
&lt;br /&gt;
 always @( ni_n2c_mes_service_valid, ni_n2c_mes_service, n2c_sync_message, bm_c2n_mes_service_consumed, sc_account_consumed, n2c_mes_service_consumed, io_intf_message_consumed ) begin&lt;br /&gt;
  ...&lt;br /&gt;
  if (ni_n2c_mes_service_valid) begin&lt;br /&gt;
    if( ni_n2c_mes_service.message_type == HOST ) begin&lt;br /&gt;
      bm_n2c_mes_service.data  = ni_n2c_mes_service.data;&lt;br /&gt;
      bm_n2c_mes_service_valid = ni_n2c_mes_service_valid;&lt;br /&gt;
      c2n_mes_service_consumed = bm_c2n_mes_service_consumed;&lt;br /&gt;
    end else if ( ni_n2c_mes_service.message_type == SYNC ) begin&lt;br /&gt;
       if ( n2c_sync_message.sync_type == ACCOUNT ) begin&lt;br /&gt;
         ni_account_mess          = n2c_sync_message.sync_mess.account_mess;&lt;br /&gt;
         ni_account_mess_valid    = ni_n2c_mes_service_valid;&lt;br /&gt;
         c2n_mes_service_consumed = sc_account_consumed;&lt;br /&gt;
    end else if ( n2c_sync_message.sync_type == RELEASE ) begin&lt;br /&gt;
         n2c_release_message      = n2c_sync_message.sync_mess.release_mess;&lt;br /&gt;
         n2c_release_valid        = ni_n2c_mes_service_valid;&lt;br /&gt;
         c2n_mes_service_consumed = n2c_mes_service_consumed;&lt;br /&gt;
    end&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
In case of SYNCH message, the above arbiter checks if the message is an ACCOUNT or a RELEASE type. In the first case, the service message is dispatched to the Synchronization core, otherwise to the NPU core on the service interface, which is directly connected to the Barrier core module allocated in the core.&lt;br /&gt;
&lt;br /&gt;
Possible incoming host requests are defined in the &amp;lt;code&amp;gt;npu_message_service_define.sv&amp;lt;/code&amp;gt; header file:&lt;br /&gt;
&lt;br /&gt;
 typedef enum logic [`MESSAGE_TYPE_LENGTH - 1 : 0] {&lt;br /&gt;
  HOST  = 0,&lt;br /&gt;
  SYNC  = 1,&lt;br /&gt;
  IO_OP = 2&lt;br /&gt;
 } service_message_type_t;&lt;br /&gt;
&lt;br /&gt;
While '''Synchronization Core''' is widely described in the [[Synchronization|Synchronization architecture]] section, in the remains of this section we will focus on the other modules that rely on the Service Virtual Channel.&lt;br /&gt;
&lt;br /&gt;
===C2N Service Scheduler===&lt;br /&gt;
This module stores and serializes requests from modules that attempt to send a message over the Service Virtual Channel. Since this channel has a single entry point, the &amp;lt;code&amp;gt;c2n_service_scheduler&amp;lt;/code&amp;gt; provides a dedicated port to each of the requiring modules and forwards their request to the service virtual channel. When a conflict occurs, this module serializes the conflicting requests, each request is selected by an internal round-robin arbiter. &lt;br /&gt;
&lt;br /&gt;
===Boot Manager===&lt;br /&gt;
Host commands are dispatched to the &amp;lt;code&amp;gt;boot_manager&amp;lt;/code&amp;gt; module interconnected with the Thread Controller and control registers on the core. This module implements core-side logic for the item interface, described in the [[MC_Item|Item Interface]] section, interacting with the core on the basis of messages from the host.&lt;br /&gt;
&lt;br /&gt;
===IO Interface===&lt;br /&gt;
The &amp;lt;code&amp;gt;io_interface&amp;lt;/code&amp;gt; module fetches memory request on the non-coherent memory region and dispatches them to the right IO device allocated within the system. This module mainly converts a memory request into an IO message that will flow over the network. For each memory request from the core, the &amp;lt;code&amp;gt;io_interface&amp;lt;/code&amp;gt; module encapsulates essential information, such as requesting address, in an IO message as follow: &lt;br /&gt;
&lt;br /&gt;
 assign io_intf_available_to_core = ~req_almost_full,&lt;br /&gt;
    req_value_i.io_source     = tile_id_t'(TILE_ID),    &lt;br /&gt;
    req_value_i.io_thread     = ldst_io_thread,&lt;br /&gt;
    req_value_i.io_operation  = io_operation_t'(ldst_io_operation),&lt;br /&gt;
    req_value_i.io_address    = ldst_io_address,&lt;br /&gt;
    req_value_i.io_data       = ldst_io_data;&lt;br /&gt;
     &lt;br /&gt;
IO messages from such a component are always forwarded to the H2C tile:&lt;br /&gt;
&lt;br /&gt;
 assign dest_tile_idx = issue_req ? `TILE_H2C_ID : saved_mess.io_source;&lt;br /&gt;
&lt;br /&gt;
== MC tile ==&lt;br /&gt;
&lt;br /&gt;
[[File:Tile mc.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The MC tile interfaces the main memory, which is defined as follow:&lt;br /&gt;
 // To Memory NI&lt;br /&gt;
 output logic  [MANGO_ADDRESS_WIDTH - 1 : 0]             n2m_request_address,&lt;br /&gt;
 output logic                       [63 : 0]             n2m_request_dirty_mask,&lt;br /&gt;
 output logic  [MANGO_DATA_WIDTH - 1 : 0]                n2m_request_data,&lt;br /&gt;
 output logic                                            n2m_request_read,&lt;br /&gt;
 output logic                                            n2m_request_write,&lt;br /&gt;
 output logic                                            n2m_request_is_instr,&lt;br /&gt;
 output logic                                            n2m_avail,&lt;br /&gt;
 &lt;br /&gt;
 // From Memory NI&lt;br /&gt;
 input  logic                                            m2n_request_read_available,&lt;br /&gt;
 input  logic                                            m2n_request_write_available,&lt;br /&gt;
 input  logic                                            m2n_response_valid,&lt;br /&gt;
 input  logic  [MANGO_ADDRESS_WIDTH - 1 : 0]             m2n_response_address,&lt;br /&gt;
 input  logic  [MANGO_DATA_WIDTH - 1 : 0]                m2n_response_data&lt;br /&gt;
&lt;br /&gt;
The memory interface has a simple valid/available handshake and allows read/write operations.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;npu2memory&amp;lt;/code&amp;gt; module turns coherence messages from the NPU core into valid memory requests for the interface above:&lt;br /&gt;
&lt;br /&gt;
 assign request_is_valid                = |request_oh &amp;amp; enable &amp;amp; ~pending_read;&lt;br /&gt;
 assign request_is_read                 = request_is_valid &amp;amp; ( pending_fwd_request_out.packet_type == FWD_GETS | pending_fwd_request_out.packet_type == FWD_GETM ) &amp;amp; grant_oh[1];&lt;br /&gt;
 assign request_is_write                = m2n_request_write_available &amp;amp; ni_response_network_available &amp;amp; ~pending_resp_fifo &amp;amp; request_is_valid &amp;amp; pending_response_out.packet_type == WB &amp;amp; grant_oh[0];&lt;br /&gt;
&lt;br /&gt;
This module also provides endianness swapping supported if required:  &lt;br /&gt;
 if (SWAPEND) begin&lt;br /&gt;
  for ( swap_word = 0; swap_word &amp;lt; 16; swap_word++ ) begin : swap_word_gen&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 +: 8]      = m2n_response_data[swap_word * 32 + 24 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 8 +: 8]  = m2n_response_data[swap_word * 32 + 16 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 16 +: 8] = m2n_response_data[swap_word * 32 + 8 +: 8];&lt;br /&gt;
     assign m2n_response_data_swap[swap_word * 32 + 24 +: 8] = m2n_response_data[swap_word * 32 +: 8];&lt;br /&gt;
 &lt;br /&gt;
     assign out_data_swap[swap_word * 32 +: 8]               = pending_response_out.data[swap_word * 32 + 24 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 8 +: 8]           = pending_response_out.data[swap_word * 32 + 16 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 16 +: 8]          = pending_response_out.data[swap_word * 32 + 8 +: 8];&lt;br /&gt;
     assign out_data_swap[swap_word * 32 + 24 +: 8]          = pending_response_out.data[swap_word * 32 +: 8];&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Parameter ''SWAPEND'' determines whether the swapping logic is allocated or not.&lt;br /&gt;
&lt;br /&gt;
== H2C tile ==&lt;br /&gt;
&lt;br /&gt;
[[File:Tile h2c.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The H2C tile interfaces the system with the host through the Item interface. The &amp;lt;code&amp;gt;npu_item_iternface&amp;lt;/code&amp;gt; interprets incoming item from the host and builds service packets marked as HOST type which encapsulates the command sent by the host. One the packet is ready, the &amp;lt;code&amp;gt;npu_item_interface&amp;lt;/code&amp;gt; forwards it to the destination tile through the service network. Section [[MC_Item|Item Interface]] details service packets sent by this module.&lt;br /&gt;
&lt;br /&gt;
== NONE tile ==&lt;br /&gt;
Empty tile, a NONE tile allocates only shared network- and coherence-related components. &lt;br /&gt;
 &lt;br /&gt;
== HT tile ==&lt;br /&gt;
Described in [[Heterogeneous Tile|Heterogeneous Tile]] section.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Network_router&amp;diff=2266</id>
		<title>Network router</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Network_router&amp;diff=2266"/>
				<updated>2019-07-02T10:34:28Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* First stage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This section describes the '''Router''' implementation. &lt;br /&gt;
&lt;br /&gt;
The router is part of a mesh network, it has an I/O port for each cardinal direction, plus the local injection/ejection port.&lt;br /&gt;
&lt;br /&gt;
Each port exchanges FLITs with neighbour routers. FLITs are routed using the XY-DOR protocol with look-ahead. This means every router calculates the next hop as if it were the next router along the path. This optimization allows us to reduce the pipeline length of the router, improving latencies.&lt;br /&gt;
&lt;br /&gt;
For coherence-related reasons, the network has four virtual channels, each classifies different packet types. Furthermore, an On/Off back-pressure for each virtual channel avoids FIFOs overflow. The following rules must be ensured:&lt;br /&gt;
* a flit cannot be routed on a different virtual channel;&lt;br /&gt;
* different packets cannot be interleaved on the same virtual channel;&lt;br /&gt;
&lt;br /&gt;
The router is implemented in a pipelined fashion. Although three stages are presented, the last one's output is not buffered. This effectively reduces the pipeline delay to two stages.&lt;br /&gt;
&lt;br /&gt;
[[File:router.jpg|1000px|router]]&lt;br /&gt;
&lt;br /&gt;
The first stage consists of input buffers, connected directly to the input ports. They are responsible for the storage of flits to be routed and for the generation of the back-pressure signals.&lt;br /&gt;
&lt;br /&gt;
The second stage is composed of three main blocks: the allocator, the flit manager and the routing logic. The allocator manages the allocation of the third stage ports, generating a grant signal for each virtual channel allowed to proceed, considering also back-pressure signals coming from other routers. This grant is used by the flit manager to select the winning flits, which are fed to the next stage along with the routing information generated by the routing block.&lt;br /&gt;
&lt;br /&gt;
The third stage is a cross-bar, allowing each of the 5 input port to access each of the 5 output port, provided that no collisions are found.&lt;br /&gt;
&lt;br /&gt;
== First stage ==&lt;br /&gt;
&lt;br /&gt;
Each input port manages a separate buffering for each virtual channel. An incoming flit carries the virtual channel ID in which it have to be enqueued. The implementation for a single virtual channel is reported below.&lt;br /&gt;
&lt;br /&gt;
[[File:first_stage.jpg|1000px|Router first stage]]&lt;br /&gt;
&lt;br /&gt;
For each virtual channel, two FIFO buffers are used: a general flit queue, which must enqueue every incoming flit, and a head queue, responsible of storing only the output port of each incoming flit. The queues have the same capacity, to account the worst case, that is, the presence of only head-tail flits.&lt;br /&gt;
&lt;br /&gt;
So the general flit queue will enqueue every incoming flit and will dequeue one as soon as the allocator grants it permission. The head queue will enqueue every incoming head or head-tail flit and will dequeue one as soon as a tail or head-tail flit gets dequeued from the general flit queue.&lt;br /&gt;
&lt;br /&gt;
The number of flits stored in the buffers is used to generate the back-pressure signals. In particular, we should guarantee that the system is packet loss-free while generating those signals. As there is one pipeline stage delay between these buffers and the previous router's allocator, we must raise the On/Off signal when the buffer has one free slot.&lt;br /&gt;
&lt;br /&gt;
FIFO instantiation code is reported below.&lt;br /&gt;
&lt;br /&gt;
 genvar                                            i;&lt;br /&gt;
 generate&lt;br /&gt;
 	for( i=0; i &amp;lt; `VC_PER_PORT; i=i + 1 ) begin : vc_loop&lt;br /&gt;
 		sync_fifo #(&lt;br /&gt;
 			.WIDTH ( $bits( flit_t )   ),&lt;br /&gt;
 			.SIZE  ( `QUEUE_LEN_PER_VC ),&lt;br /&gt;
 			.ALMOST_FULL_THRESHOLD ( `QUEUE_LEN_PER_VC - 1 ) )&lt;br /&gt;
 		flit_fifo (&lt;br /&gt;
 			.clk         ( clk                           ),&lt;br /&gt;
 			.reset       ( reset                         ),&lt;br /&gt;
 			.flush_en    (                               ),&lt;br /&gt;
 			.full        (                               ),&lt;br /&gt;
 			.almost_full ( on_off_out[i]                 ),&lt;br /&gt;
 			.enqueue_en  ( wr_en_in &amp;amp; flit_in.vc_id == i ),&lt;br /&gt;
 			.value_i     ( flit_in                       ),&lt;br /&gt;
 			.empty       ( ip_empty[i]                   ),&lt;br /&gt;
 			.almost_empty(                               ),&lt;br /&gt;
 			.dequeue_en  ( sa_grant[i]                   ),&lt;br /&gt;
 			.value_o     ( ip_flit_in_mux[i]             )&lt;br /&gt;
 		);&lt;br /&gt;
 &lt;br /&gt;
 		sync_fifo #(&lt;br /&gt;
 			.WIDTH ( $bits( port_t ) ),&lt;br /&gt;
 			.SIZE ( `QUEUE_LEN_PER_VC ) )&lt;br /&gt;
 		header_fifo (&lt;br /&gt;
 			.clk         ( clk                                                                                       ),&lt;br /&gt;
 			.reset       ( reset                                                                                     ),&lt;br /&gt;
 			.flush_en    (                                                                                           ),&lt;br /&gt;
 			.full        (                                                                                           ),&lt;br /&gt;
 			.almost_full (                                                                                           ),&lt;br /&gt;
 			.enqueue_en  ( wr_en_in &amp;amp; flit_in.vc_id == i &amp;amp; ( flit_in.flit_type == HEADER | flit_in.flit_type == HT ) ),&lt;br /&gt;
 			.value_i     ( flit_in.next_hop_port                                                                     ),&lt;br /&gt;
 			.empty       ( request_not_valid[i]                                                                      ),&lt;br /&gt;
 			.almost_empty(                                                                                           ),&lt;br /&gt;
 			.dequeue_en  ( ( ip_flit_in_mux[i].flit_type == TAIL | ip_flit_in_mux[i].flit_type == HT ) &amp;amp; sa_grant[i] ),&lt;br /&gt;
 			.value_o     ( dest_port_app                                                                             )&lt;br /&gt;
 		);&lt;br /&gt;
&lt;br /&gt;
As stated before, this will work as long as packets don't interleave on the same virtual channel.&lt;br /&gt;
&lt;br /&gt;
== Second stage ==&lt;br /&gt;
&lt;br /&gt;
The second stage takes as input requests coming from each virtual channel, along with back-pressure signals sent by other routers. Its main goals are:&lt;br /&gt;
* allocate resources, which in this case are crossbar input ports;&lt;br /&gt;
* properly update the routing information for each flits passing through.&lt;br /&gt;
&lt;br /&gt;
The overall implementation is reported below.&lt;br /&gt;
&lt;br /&gt;
[[File:second_stage.jpg|800px|Router second stage]]&lt;br /&gt;
&lt;br /&gt;
Grant signals are also fed-back to the first stage, to allow flits to be dequeued.&lt;br /&gt;
&lt;br /&gt;
Look-ahead routing logic is replicated for each input port. This way, as soon as we know which virtual channel has been granted access for each input port, we can calculate the routing information.&lt;br /&gt;
&lt;br /&gt;
=== Allocator implementation ===&lt;br /&gt;
&lt;br /&gt;
The allocator is required, as we have a request for every virtual channel storing valid flits, each requiring access to an output port. As the third stage has only five input ports, and only one is allowed to access a given output port, requests must be scheduled.&lt;br /&gt;
&lt;br /&gt;
[[File:allocation.jpg|800px|Allocation]]&lt;br /&gt;
&lt;br /&gt;
The allocator can be decomposed in two parts:&lt;br /&gt;
* virtual channel allocator, which ensures that for every output virtual channel only one input virtual channel is granted access to it;&lt;br /&gt;
* switch allocator, which first ensures that for every input port there is only one virtual channel allowed to access the crossbar, and then ensures that for every output port there is only one input port allowed to access the crossbar.&lt;br /&gt;
&lt;br /&gt;
==== Virtual channel allocator ====&lt;br /&gt;
&lt;br /&gt;
Virtual channel allocation is implemented using a round-robin arbiter, with additional grant-and-hold circuitry (as implemented by the [[#Allocator core|allocator core]] module). Once an input virtual channel has been granted access to an output virtual channel, the grant will be held until its request is fulfilled. This is to ensure that multiple packets do not interleave on the same virtual channel.&lt;br /&gt;
&lt;br /&gt;
Back-pressure signals from other routers are used in this stage. &lt;br /&gt;
&lt;br /&gt;
The output grant signals are generated on a virtual-channel basis and fed to the next stage.&lt;br /&gt;
&lt;br /&gt;
==== Switch allocator ====&lt;br /&gt;
&lt;br /&gt;
The switch allocator has two roles:&lt;br /&gt;
* it chooses, for each port's candidate virtual channels, which one is granted access;&lt;br /&gt;
* it chooses, if there are any input ports requesting access to the same output port, which one is granted access.&lt;br /&gt;
&lt;br /&gt;
The first part is implemented using a round-robin arbiter, allowing flits coming from different virtual channels to be interleaved on the same output port, as the first stage has already ensured that no two virtual channels have been granted access to the same output virtual channel.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	for( i=0; i &amp;lt; `PORT_NUM; i=i + 1 ) begin : sw_allocation_loop&lt;br /&gt;
 &lt;br /&gt;
 		assign va_grant_per_port[i] = {`PORT_NUM{| ( va_grant[i] &amp;amp; ~ip_empty[i] )}};&lt;br /&gt;
 &lt;br /&gt;
 		rr_arbiter #(&lt;br /&gt;
 			.NUM_REQUESTERS( `VC_PER_PORT ) )&lt;br /&gt;
 		sa_arbiter (&lt;br /&gt;
 			.clk       ( clk                               ),&lt;br /&gt;
 			.reset     ( reset                             ),&lt;br /&gt;
 			.request   ( va_grant[i] &amp;amp; ~ip_empty[i] ),&lt;br /&gt;
 			.update_lru( 1'b1                              ),&lt;br /&gt;
 			.grant_oh  ( grant_to_mux_ip[i]                )&lt;br /&gt;
 		);&lt;br /&gt;
  &lt;br /&gt;
 		mux_npu #(&lt;br /&gt;
 			.N    ( `VC_PER_PORT ),&lt;br /&gt;
 			.WIDTH( `PORT_NUM    )&lt;br /&gt;
 		)&lt;br /&gt;
 		sa_mux (&lt;br /&gt;
 			.onehot( grant_to_mux_ip[i] ),&lt;br /&gt;
 			.i_data( ip_dest_port[i]    ),&lt;br /&gt;
 			.o_data( sa_port[i]         )&lt;br /&gt;
 		);&lt;br /&gt;
 	end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
At this point, we can proceed with the second part, and we also have all the required information to perform the routing calculation. In fact, we know for each input port which virtual channel has been granted access.&lt;br /&gt;
&lt;br /&gt;
The second part is implemented again with a round-robin arbiter and a grant-and-hold circuitry, using [[#Allocator core|the allocator core]]. It guarantees that now we have no conflicts on switch allocation, and we can use this grant signals to set up the crossbar.&lt;br /&gt;
&lt;br /&gt;
==== Allocator core ====&lt;br /&gt;
&lt;br /&gt;
The allocator core is a generic round-robin allocator, composed of a parameterizable number of parallel arbiters in which the input and output are properly scrambled and the output are or-ed to obtain the grant signals.&lt;br /&gt;
&lt;br /&gt;
[[File:allocatore_core.png|400px|allocatore_core]]&lt;br /&gt;
&lt;br /&gt;
 module allocator_core #(&lt;br /&gt;
 		parameter N    = 5,  // input ports&lt;br /&gt;
 		parameter M    = 4,  // virtual channels per port&lt;br /&gt;
 		parameter SIZE = 5 ) // output ports&lt;br /&gt;
 	(&lt;br /&gt;
 		input                                             clk,&lt;br /&gt;
 		input                                             reset,&lt;br /&gt;
 		input  logic [N - 1 : 0][M - 1 : 0][SIZE - 1 : 0] request,&lt;br /&gt;
 		input  logic [N - 1 : 0][M - 1 : 0]               on_off,&lt;br /&gt;
 		output logic [N - 1 : 0][M - 1 : 0]               grant&lt;br /&gt;
 	);&lt;br /&gt;
&lt;br /&gt;
It also provides a grant-and-hold circuitry.&lt;br /&gt;
&lt;br /&gt;
=== Look-ahead routing ===&lt;br /&gt;
&lt;br /&gt;
The look-ahead routing calculates the output port as if it were the next router on the path. This allows to do it in parallel with the switch allocator, as the switch allocator doesn't need the routing output. Otherwise, an additional pipeline stage for routing calculation would be needed.&lt;br /&gt;
&lt;br /&gt;
=== Flit manager ===&lt;br /&gt;
&lt;br /&gt;
The flit manager selects, for each port, the flit which has access to the crossbar inputs. It also modifies the flit to include the routing results.&lt;br /&gt;
&lt;br /&gt;
 for( i=0; i &amp;lt; `PORT_NUM; i=i + 1 ) begin : flit_loop&lt;br /&gt;
 	always_comb begin&lt;br /&gt;
 		flit_in_granted_mod[i] = flit_in_granted[i];&lt;br /&gt;
 			if ( flit_in_granted[i].flit_type == HEADER || flit_in_granted[i].flit_type == HT )&lt;br /&gt;
 				flit_in_granted_mod[i].next_hop_port = port_t'( lk_next_port[i] );&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== Third stage ==&lt;br /&gt;
&lt;br /&gt;
The third stage is a crossbar, connecting 5 input ports to respectively 5 output ports.&lt;br /&gt;
&lt;br /&gt;
It has been implemented as a mux for each output port, and selection signals are obtained from the second stage outputs.&lt;br /&gt;
&lt;br /&gt;
The output of this stage is not buffered. This means that in practice, the clock cycles needed to completely process a packet reduce to two.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Network_interface&amp;diff=2265</id>
		<title>Network interface</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Network_interface&amp;diff=2265"/>
				<updated>2019-07-02T10:05:59Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Rebuilt packets queue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The network interface implementation is discussed in this page.&lt;br /&gt;
&lt;br /&gt;
The Network Interface (NI) has the role of abstracting the network communication details, providing a high-level interface to all the components in the tile which require communication. The Router Interface splits application messages (such as coherence related messages) into multiple flits to be injected in the local router.&lt;br /&gt;
&lt;br /&gt;
The virtual channels used by tile components are reported below.&lt;br /&gt;
&lt;br /&gt;
[[File:NI_VN.jpg|600px|Virtual channel usage]]&lt;br /&gt;
&lt;br /&gt;
Due to the coherence protocol implemented, both the directory controller and the cache controller might require access to the Response virtual channel. This is carefully handled by the network interface, which stores incoming requests in FIFOs and arbitrating concurrent requests.&lt;br /&gt;
&lt;br /&gt;
Another main feature supported is multicast addressing. The directory controller, in some cases, might send the same packet to multiple recipients (e.g. ACKs). In the current implementation, it is handled by the Network Interface as a sequence of unicast messages.&lt;br /&gt;
&lt;br /&gt;
== General architecture ==&lt;br /&gt;
&lt;br /&gt;
The network interface has a regular internal structure. This is because ejection and injection services are implemented as two distinct functionalities, allowing a modular design. Moreover, the ejection and injection logics are almost the same for every virtual channel.&lt;br /&gt;
&lt;br /&gt;
For this reason, two separated modules are provided, that together handle the whole network communication ''for a single virtual channel'':&lt;br /&gt;
* &amp;lt;code&amp;gt;virtual_network_core_to_net&amp;lt;/code&amp;gt;, which handles flit injection;&lt;br /&gt;
* &amp;lt;code&amp;gt;virtual_network_net_to_core&amp;lt;/code&amp;gt;, which handles ejection.&lt;br /&gt;
&lt;br /&gt;
Both of them are parameterized and easily adapt to different virtual channel needs. In particular, the virtual channels number and the packet length must be specified.&lt;br /&gt;
&lt;br /&gt;
As the router ejects flits toward the tile, only the network interface assigned to that specific virtual channel reassembles them back and buffers the reconstructed packet until the corresponding component is ready to work. The virtual channel 0 instantiation is reported below.&lt;br /&gt;
&lt;br /&gt;
 // --- Request Virtual Network VC0 --- //&lt;br /&gt;
 virtual_network_net_to_core # (&lt;br /&gt;
 	.VCID             ( VC0                                   ),&lt;br /&gt;
 	.PACKET_BODY_SIZE ( $bits ( coherence_request_message_t ) ),&lt;br /&gt;
 	.FLIT_NUMB        ( `RESP_FLIT_NUMB                       ),&lt;br /&gt;
 	...&lt;br /&gt;
 	...&lt;br /&gt;
 )&lt;br /&gt;
 request_virtual_network_net_to_core (&lt;br /&gt;
 	...&lt;br /&gt;
 	//Cache Controller interface&lt;br /&gt;
 	.vn_ntc_packet_out    ( ni_request           ),&lt;br /&gt;
 	.vn_ntc_packet_valid  ( ni_request_valid     ),&lt;br /&gt;
 	.core_packet_consumed ( dc_request_consumed  ),&lt;br /&gt;
 	//Router interface&lt;br /&gt;
 	.vn_ntc_credit        ( ni_credit[VC0]       ),&lt;br /&gt;
 	.router_flit_valid    ( router_flit_in_valid ),&lt;br /&gt;
 	.router_flit_in       ( router_flit_in       )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Note that the packet body size parameter is linked with the flit number parameter, but the module handles them separately.&lt;br /&gt;
&lt;br /&gt;
On the other side, the injection logic buffers outgoing packets, splitting them into flits and competes with the others VCs to obtain access to the unique router local port. To grant access to the local injection port, a round-robin arbiter with a grant-and-hold circuitry has been developed. The granted virtual channel index is used as a selector in a multiplexer which sends the right flit to the router.&lt;br /&gt;
&lt;br /&gt;
 assign vno_requests =&lt;br /&gt;
 	{vn_packet_pending[ VC3 ] &amp;amp; ~router_credit[VC3],&lt;br /&gt;
 		vn_packet_pending[ VC2 ] &amp;amp; ~router_credit[VC2],&lt;br /&gt;
 		vn_packet_pending[ VC1 ] &amp;amp; ~router_credit[VC1],&lt;br /&gt;
 		vn_packet_pending[ VC0 ] &amp;amp; ~router_credit[VC0]};&lt;br /&gt;
 &lt;br /&gt;
 rr_arbiter # (&lt;br /&gt;
 	.NUM_REQUESTERS ( `VC_PER_PORT )&lt;br /&gt;
 )&lt;br /&gt;
 ni_request_rr_arbiter (&lt;br /&gt;
 	.clk        ( clk          ),&lt;br /&gt;
 	.reset      ( reset        ),&lt;br /&gt;
 	.request    ( vno_requests ),&lt;br /&gt;
 	.update_lru ( 1'b1         ),&lt;br /&gt;
 	.grant_oh   ( vno_granted  )&lt;br /&gt;
 ) ;&lt;br /&gt;
 &lt;br /&gt;
 oh_to_idx # (&lt;br /&gt;
 	.NUM_SIGNALS ( `VC_PER_PORT ),&lt;br /&gt;
 	.DIRECTION   ( &amp;quot;LSB0&amp;quot;       )&lt;br /&gt;
 )&lt;br /&gt;
 ni_request_grant_oh_to_idx (&lt;br /&gt;
 	.one_hot ( vno_granted    ),&lt;br /&gt;
 	.index   ( vco_granted_id )&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 assign ni_flit_out    = vn_flit_out[vco_granted_id],&lt;br /&gt;
 	ni_flit_out_valid = vn_flit_valid[vco_granted_id];&lt;br /&gt;
&lt;br /&gt;
Special care must be given to the response virtual channel. Two injection and two ejection modules are instanced for this virtual channel, each interfacing respectively with the cache controller and the directory controller.&lt;br /&gt;
&lt;br /&gt;
The ejection module supports a specific parameter to let it know if it is interfacing the directory or the cache controller. When this parameter is set, it will check the core_destination field in the flit (see [[Network#Data structures|flit structure]]) to know if it should ignore it.&lt;br /&gt;
&lt;br /&gt;
The injection modules are not aware of who is using them. For this reason, both will compete to be granted access to the virtual channel (and the winner will compete with the others to access router input port). A round-robin arbiter with a grant-and-hold circuitry is used. This will ensure that once one of the controllers gains access to the virtual channel, it will retain it until the full request has been sent.&lt;br /&gt;
&lt;br /&gt;
 // each bit is high respectively if the Cache Controller or the Directory Controller wants to inject a packet&lt;br /&gt;
 assign pending_tmp = {response_in[CC_ID].vn_packet_pending, response_in[DC_ID].vn_packet_pending};&lt;br /&gt;
 &lt;br /&gt;
 // the arbiter chooses among the two of them&lt;br /&gt;
 grant_hold_rr_arbiter #(&lt;br /&gt;
 	.NUM_REQUESTERS( 2 )&lt;br /&gt;
 )&lt;br /&gt;
 response_vn_rr_arbiter (&lt;br /&gt;
 	.clk      ( clk                  ),&lt;br /&gt;
 	.reset    ( reset                ),&lt;br /&gt;
 	.request  ( pending_tmp          ),&lt;br /&gt;
 	.hold_in  ( pending_tmp          ),&lt;br /&gt;
 	.grant_oh ( response_vn_grant_oh )&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 // the arbitration result is used to select the winning flit, and the signals are updated accordingly&lt;br /&gt;
 assign vn_packet_pending[ VC1 ] = |response_vn_grant_oh ;&lt;br /&gt;
 assign vn_flit_out[VC1]         = response_vn_grant_oh[0]? response_in[DC_ID].vn_flit_out   : response_in[CC_ID].vn_flit_out;&lt;br /&gt;
 assign vn_flit_valid[VC1]       = response_vn_grant_oh[0]? response_in[DC_ID].vn_flit_valid : response_in[CC_ID].vn_flit_valid;&lt;br /&gt;
&lt;br /&gt;
[[File:NI.png|900px|Network Interface]]&lt;br /&gt;
&lt;br /&gt;
== Network to core module ==&lt;br /&gt;
&lt;br /&gt;
This module is composed of two parts: a control unit which handles the incoming flits, rebuilding them as a packet; and a queue of rebuilt packets.&lt;br /&gt;
&lt;br /&gt;
=== Control unit ===&lt;br /&gt;
&lt;br /&gt;
The control unit has a logic based on registers used to store temporary results of the rebuilding process.&lt;br /&gt;
&lt;br /&gt;
Each register tracks the count of flits already received, and uses this count to merge them into a packet.&lt;br /&gt;
&lt;br /&gt;
 logic       [$clog2( FLIT_NUMB ) - 1 : 0] count;&lt;br /&gt;
 &lt;br /&gt;
 flit_body_t [FLIT_NUMB - 1 : 0]           rebuilt_packet;&lt;br /&gt;
 &lt;br /&gt;
 ...&lt;br /&gt;
 &lt;br /&gt;
 if (router_flit_valid) begin&lt;br /&gt;
 	rebuilt_packet[count] &amp;lt;= router_flit_in.payload;&lt;br /&gt;
 	&lt;br /&gt;
 	if (router_flit_in.flit_type == TAIL || router_flit_in.flit_type == HT) begin&lt;br /&gt;
 		count &amp;lt;= '{default: '0};&lt;br /&gt;
 		cu_packet_rebuilt_compl &amp;lt;= 1'b1;&lt;br /&gt;
 	end else&lt;br /&gt;
 		count &amp;lt;= count + 1;&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Please note that the control logic also detects if the flits are for the cache controller or to the directory controller, that are connected on the same response virtual channel.&lt;br /&gt;
 		&lt;br /&gt;
 	if (router_flit_in.flit_type == HEADER || router_flit_in.flit_type == HT) begin&lt;br /&gt;
 		cu_is_for_cc &amp;lt;= router_flit_in.core_destination == TO_CC;&lt;br /&gt;
 		cu_is_for_dc &amp;lt;= router_flit_in.core_destination == TO_DC;&lt;br /&gt;
 	end &lt;br /&gt;
&lt;br /&gt;
=== Rebuilt packets queue ===&lt;br /&gt;
&lt;br /&gt;
Rebuilt packets are stored in a FIFO, so we can enqueue multiple requests. When the receiver component is ready to handle the request, it will assert the core_packet_consumed signal, freeing one buffer slot.&lt;br /&gt;
&lt;br /&gt;
The back-pressure signals will be raised when there are two free buffer slots. This accounts for the worst case, when there is a sequence of 1-flit packets incoming, some of which are yet in the pipe stages and should not be lost. The pipe stages in between are two: the router crossbar and the control unit of this module.&lt;br /&gt;
&lt;br /&gt;
 sync_fifo #(&lt;br /&gt;
 	.WIDTH                 ( PACKET_BODY_SIZE     ),&lt;br /&gt;
 	.SIZE                  ( PACKET_FIFO_SIZE     ),&lt;br /&gt;
 	.ALMOST_FULL_THRESHOLD ( PACKET_FIFO_SIZE - 2 ) &lt;br /&gt;
 )&lt;br /&gt;
 rebuilt_packet_fifo (&lt;br /&gt;
 	...&lt;br /&gt;
 	.almost_full ( packet_alm_fifo_full      ),&lt;br /&gt;
 	.enqueue_en  ( enqueue_en                ),&lt;br /&gt;
 	.value_i     ( cu_rebuilt_packet         ),&lt;br /&gt;
 	.empty       ( rebuilt_packet_fifo_empty ),&lt;br /&gt;
 	.almost_empty(                           ),&lt;br /&gt;
 	.dequeue_en  ( core_packet_consumed      ),&lt;br /&gt;
 	.value_o     ( vn_ntc_packet_out         )&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 assign vn_ntc_credit       = packet_alm_fifo_full;&lt;br /&gt;
&lt;br /&gt;
The enqueue signal is generated from the incoming flit virtual channel ID. For the reason explained above, the only exception are that of cache and directory controllers. In that case, the module will also check that the flit is for the cache/directory controller, and it will enqueue it based on the TYPE parameter.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	if ( TYPE == &amp;quot;CC&amp;quot; )&lt;br /&gt;
 		assign&lt;br /&gt;
 			enqueue_en    = cu_packet_rebuilt_compl &amp;amp; cu_is_for_cc;&lt;br /&gt;
 	else if ( TYPE == &amp;quot;DC&amp;quot; )&lt;br /&gt;
 		assign enqueue_en = cu_packet_rebuilt_compl &amp;amp; cu_is_for_dc;&lt;br /&gt;
 	else&lt;br /&gt;
 		assign enqueue_en = cu_packet_rebuilt_compl;&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
If the incoming packet arrives as this sequence of flits:&lt;br /&gt;
&lt;br /&gt;
 1st Flit = {FLIT_TYPE_HEAD, FLIT_BODY_SIZE'h20}&lt;br /&gt;
 2nd Flit = {FLIT_TYPE_BODY, FLIT_BODY_SIZE'h40}&lt;br /&gt;
 3rd Flit = {FLIT_TYPE_BODY, FLIT_BODY_SIZE'h60}&lt;br /&gt;
 4th Flit = {FLIT_TYPE_TAIL, FLIT_BODY_SIZE'h10};&lt;br /&gt;
&lt;br /&gt;
The rebuilt packet passed to the Cache Controller is:&lt;br /&gt;
&lt;br /&gt;
 Packet = {FLIT_BODY_SIZE'h10, FLIT_BODY_SIZE'h60, FLIT_BODY_SIZE'h40, FLIT_BODY_SIZE'h20}&lt;br /&gt;
&lt;br /&gt;
== Core to network module ==&lt;br /&gt;
&lt;br /&gt;
This module splits a packet into flits and sends them to the router local port. It also supports multicasting, implemented as multiple unicast messages. It is composed of two parts: a packet queue, and a control unit which handles the outgoing flits.&lt;br /&gt;
&lt;br /&gt;
=== Request queue ===&lt;br /&gt;
&lt;br /&gt;
Incoming requests are enqueued in a FIFO as the control unit handles them. It also provides stop signals for the requesting device, in case the FIFO gets full.&lt;br /&gt;
&lt;br /&gt;
The enqueued structure is composed of a packet body along with all the recipients of the message.&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	logic [PACKET_BODY_SIZE - 1 : 0] packet_body;&lt;br /&gt;
 	logic packet_has_data;&lt;br /&gt;
 	tile_address_t [DEST_NUMB - 1 : 0 ] packet_destinations;&lt;br /&gt;
 	logic [DEST_NUMB - 1 : 0 ] packet_destinations_valid;&lt;br /&gt;
 } packet_information_t;&lt;br /&gt;
&lt;br /&gt;
A request will be enqueued when the requester asserts the &amp;lt;code&amp;gt;packet_valid&amp;lt;/code&amp;gt; signal, and the head of the FIFO will be dequeued when the control unit notifies completion.&lt;br /&gt;
&lt;br /&gt;
 sync_fifo # (&lt;br /&gt;
 	.WIDTH                 ( $bits ( packet_information_t ) ),&lt;br /&gt;
 	.SIZE                  ( PACKET_FIFO_SIZE               ),&lt;br /&gt;
 	.ALMOST_FULL_THRESHOLD ( PACKET_ALMOST_FULL_THRESHOLD   )&lt;br /&gt;
 )&lt;br /&gt;
 packet_in_fifo (&lt;br /&gt;
 	...&lt;br /&gt;
 	.almost_full  ( vn_packet_fifo_full    ),&lt;br /&gt;
 	.enqueue_en   ( packet_valid           ),&lt;br /&gt;
 	.value_i      ( packet_information_in  ),&lt;br /&gt;
 	.empty        ( packet_fifo_empty      ),&lt;br /&gt;
 	.almost_empty (                        ),&lt;br /&gt;
 	.dequeue_en   ( cu_packet_dequeue      ),&lt;br /&gt;
 	.value_o      ( packet_information_out )&lt;br /&gt;
 ) ;&lt;br /&gt;
 &lt;br /&gt;
Request signals are generated, which allow this module to compete for router port access.&lt;br /&gt;
&lt;br /&gt;
 assign packet_pending                               = ~packet_fifo_empty;&lt;br /&gt;
&lt;br /&gt;
=== Control unit ===&lt;br /&gt;
&lt;br /&gt;
Control unit's responsibilities are:&lt;br /&gt;
* split a packet into flits;&lt;br /&gt;
* determine the type (head, body, tail, head-tail) of each flit;&lt;br /&gt;
* calculate a pre-routing of the outgoing flits, as routers implement routing look-ahead (see [[Network]]);&lt;br /&gt;
* properly handle multicast messages, if required;&lt;br /&gt;
* account for router back-pressure signals;&lt;br /&gt;
&lt;br /&gt;
 control_unit_packet_to_flit # (&lt;br /&gt;
 	parameter DEST_OH          = &amp;quot;TRUE&amp;quot;,&lt;br /&gt;
 	...&lt;br /&gt;
 	parameter PACKET_BODY_SIZE = 256,&lt;br /&gt;
 	parameter DEST_NUMB        = 4 )&lt;br /&gt;
 (&lt;br /&gt;
 	...&lt;br /&gt;
 	input  logic                                                packet_valid,&lt;br /&gt;
 	input  logic                                                packet_has_data,&lt;br /&gt;
 	input  tile_address_t [DEST_NUMB - 1 : 0]                   packet_destinations,&lt;br /&gt;
 	input  logic          [DEST_NUMB - 1 : 0]                   packet_destinations_valid,&lt;br /&gt;
 &lt;br /&gt;
 	input  logic                                                flit_credit,&lt;br /&gt;
 &lt;br /&gt;
 	output logic          [$clog2 ( PACKET_BODY_SIZE ) - 1 : 0] cu_packet_chunck_sel,&lt;br /&gt;
 	output logic                                                cu_flit_valid,&lt;br /&gt;
 	output flit_header_t                                        cu_flit_out_header,&lt;br /&gt;
 	output logic                                                cu_packet_dequeue&lt;br /&gt;
 &lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
A request is considered fulfilled when a unicast message has been sent to all its recipients. Recipients are served in a round-robin fashion.&lt;br /&gt;
&lt;br /&gt;
 rr_arbiter # (&lt;br /&gt;
 	.NUM_REQUESTERS ( DEST_NUMB )&lt;br /&gt;
 )&lt;br /&gt;
 rr_arbiter (&lt;br /&gt;
 	.clk        ( clk                  ) ,&lt;br /&gt;
 	.reset      ( reset                ) ,&lt;br /&gt;
 	.request    ( packet_dest_pending  ) ,&lt;br /&gt;
 	.update_lru ( 1'b0                 ) ,&lt;br /&gt;
 	.grant_oh   ( destination_grant_oh )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Already served recipients are stored in a bit mask, which gets updated with the grant signal after each sending.&lt;br /&gt;
&lt;br /&gt;
 dest_served &amp;lt;= dest_served | destination_grant_oh;&lt;br /&gt;
&lt;br /&gt;
This bit mask tracks remaining recipients.&lt;br /&gt;
&lt;br /&gt;
 assign packet_dest_pending                 = packet_destinations_valid &amp;amp; ~dest_served;&lt;br /&gt;
 assign packet_has_dest_pending             = |packet_dest_pending;&lt;br /&gt;
&lt;br /&gt;
Routing is done for each recipient, based on the destination address.&lt;br /&gt;
&lt;br /&gt;
The parameter DEST_OH determines how multicast addresses are generated. If DEST_OH is true, the module will consider the input signal packet_destination as a one-hot encoded bit mask, where every tile has a corresponding position in this mask. Otherwise, if DEST_OH is false, multicast addresses are passed to the module into the packet_destinations input signal, and packet_destinations_valid is used to know which position into the array contains a valid address.&lt;br /&gt;
&lt;br /&gt;
 generate&lt;br /&gt;
 	if ( DEST_OH == &amp;quot;TRUE&amp;quot; ) begin&lt;br /&gt;
 		assign&lt;br /&gt;
 			real_dest.x  = destination_grant_id[`TOT_X_NODE_W - 1 : 0 ],&lt;br /&gt;
 			real_dest.y  = destination_grant_id[`TOT_Y_NODE_W + `TOT_X_NODE_W - 1 : `TOT_X_NODE_W];&lt;br /&gt;
 	end else begin&lt;br /&gt;
 		assign real_dest = packet_destinations[destination_grant_id];&lt;br /&gt;
 	end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
A finite state machine handles the remaining required actions, keeping count of how many flits have been sent up until now, and choosing the flit payload among the current packet chunk to be sent.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=L1_Cache_Controller&amp;diff=2264</id>
		<title>L1 Cache Controller</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=L1_Cache_Controller&amp;diff=2264"/>
				<updated>2019-07-01T13:21:19Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Stall Protocol ROMs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Cache controller manages the L1 cache. In particular, it handles only coherence information (such as states) since L1 data cache is managed by load/store unit.&lt;br /&gt;
&lt;br /&gt;
[[File:l1_cache.jpg|800px|LDST_CC]]&lt;br /&gt;
&lt;br /&gt;
The component is composed of 4 stages:&lt;br /&gt;
&lt;br /&gt;
* stage 1: schedules a pending request to issue (from local core or network);&lt;br /&gt;
* stage 2: contains coherence cache and MSHR;&lt;br /&gt;
* stage 3: processes a request properly with coherence protocol;&lt;br /&gt;
* stage 4: prepares coherence request/response to be sent on the network.&lt;br /&gt;
&lt;br /&gt;
All these stages are represented in the figure below. The component has been realized in a ''pipelined'' fashion in order for the controller to be able to serve multiple requests at the same time.&lt;br /&gt;
&lt;br /&gt;
=== Assumptions ===&lt;br /&gt;
The design has been driven by these assumptions:&lt;br /&gt;
&lt;br /&gt;
* cache controller schedules a request only when no other requests with on the same address are pending in the pipeline;&lt;br /&gt;
* coherence transactions are at the memory blocks level;&lt;br /&gt;
* cache controller does not schedule requests when another is in the pipeline and have the same set;&lt;br /&gt;
* only requests from the local core (load, store, replacement) allocate MSHR entries;&lt;br /&gt;
* information regarding cache block in a non-stable state are stored into the MSHR.&lt;br /&gt;
&lt;br /&gt;
Two requests on the same block cannot be issued one after another; when the first request is issued, it may modify an MSHR entry after two clock cycles (stage 3), hence the second request may read a non-up-to-date entry.&lt;br /&gt;
&lt;br /&gt;
== Stage 1 ==&lt;br /&gt;
Stage 1 is responsible for the scheduling of requests into the controller. A request could be a load miss, store miss, flush and replacement request from the local core or a coherence forwarded request or response from the network interface.&lt;br /&gt;
&lt;br /&gt;
=== MSHR Signals ===&lt;br /&gt;
The arbiter in the first stage checks if a pending request can be issued, in order to eligible for scheduling no other requests on the '''same block''' should be ''ongoing'' (or under elaboration) in the cache controller. Ongoing requests are stored in the MSHR table. Tags and sets are provided by the MSHR for each type of pending requests and are forwarded to the arbiter at Stage 1. The arbiter uses the information provided on ongoing transactions to select a pending request. The MSHR provides a look-up port for each type of request, a &amp;lt;code&amp;gt;hit&amp;lt;/code&amp;gt; single is provided along, the request is considered valid in the MSHR if such a signal is asserted: &lt;br /&gt;
&lt;br /&gt;
 // Signals to MSHR&lt;br /&gt;
 assign cc1_mshr_lookup_tag[MSHR_LOOKUP_PORT_LOAD ]   = ci_load_request_address.tag;&lt;br /&gt;
 assign cc1_mshr_lookup_set[MSHR_LOOKUP_PORT_LOAD ]   = ci_load_request_address.index;&lt;br /&gt;
 &lt;br /&gt;
 // Signals from MSHR&lt;br /&gt;
 assign load_mshr_hit                                 = cc2_mshr_lookup_hit[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
 assign load_mshr_index                               = cc2_mshr_lookup_index[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
 assign load_mshr                                     = cc2_mshr_lookup_entry_info[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
&lt;br /&gt;
=== Stall Protocol ROMs ===&lt;br /&gt;
In order to be compliant with the coherence protocol, all incoming requests on blocks in a non-stable state might be stalled. This task is performed through a series of protocol ROMs (one for each request type) that output state when the issue of relative request ought to be stalled, e.g. when a block is in state SM_A and a ''Fwd_GetS'', ''Fwd_GetM'', ''recall'', ''flush'', ''store'' or ''replacement'' request for the same block is received. In order to assert this signal, the protocol ROM needs the type of the request and the actual state of the block. The module &amp;lt;code&amp;gt;stall_protocol_rom&amp;lt;/code&amp;gt; implements this logic:&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom load_stall_protocol_rom (&lt;br /&gt;
   .current_request ( load            ),&lt;br /&gt;
   .current_state   ( load_mshr.state ),&lt;br /&gt;
   .pr_output_stall ( stall_load      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom store_stall_protocol_rom (&lt;br /&gt;
    .current_request ( store            ),&lt;br /&gt;
    .current_state   ( store_mshr.state ),&lt;br /&gt;
    .pr_output_stall ( stall_store      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom flush_stall_protocol_rom (&lt;br /&gt;
    .current_request ( flush            ),&lt;br /&gt;
    .current_state   ( flush_mshr.state ),&lt;br /&gt;
    .pr_output_stall ( stall_flush      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom replacement_stall_protocol_rom (&lt;br /&gt;
     .current_request ( replacement            ),&lt;br /&gt;
     .current_state   ( replacement_mshr.state ),&lt;br /&gt;
     .pr_output_stall ( stall_replacement      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom forwarded_stall_protocol_rom (&lt;br /&gt;
    .current_request ( fwd_2_creq( ni_forwarded_request.packet_type ) ),&lt;br /&gt;
    .current_state   ( forwarded_request_mshr.state                   ),&lt;br /&gt;
    .pr_output_stall ( stall_forwarded_request                        )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Note that response messages are never stalled in the coherence protocol, such requests are stalled only if a pending request with the same set index is already in the pipeline:&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_response                                      = ni_response_valid &amp;amp;&lt;br /&gt;
 	!(&lt;br /&gt;
 		( cc2_pending_valid &amp;amp;&amp;amp; ( ni_response.memory_address.index == cc2_pending_address.index ) ) ||&lt;br /&gt;
 		( cc3_pending_valid &amp;amp;&amp;amp; ( ni_response.memory_address.index == cc3_pending_address.index ) )&lt;br /&gt;
 	);&lt;br /&gt;
&lt;br /&gt;
=== Issuing a Request ===&lt;br /&gt;
In order to issue a generic request, it is required that:&lt;br /&gt;
&lt;br /&gt;
* MSHR has no pending requests for the same block;&lt;br /&gt;
* if the request is already in MSHR it has to be not valid;&lt;br /&gt;
* if the request is already in MSHR and valid it must not have been stalled by the protocol ROM (see [[L1 Cache Controller#Stall Protocol ROMs | stall signals]]).&lt;br /&gt;
* further stages are not serving a request on the same address (see [[L1 Cache Controller#Assumptions | assumptions]]);&lt;br /&gt;
* network interface is available;&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_load = ci_load_request_valid &amp;amp;&amp;amp; &lt;br /&gt;
 &lt;br /&gt;
        ( !load_mshr_hit || &lt;br /&gt;
             ( load_mshr_hit &amp;amp;&amp;amp; !load_mshr.valid) ||&lt;br /&gt;
              ( load_mshr_hit &amp;amp;&amp;amp; load_mshr.valid  &amp;amp;&amp;amp; load_mshr.address.tag == ci_load_request_address.tag &amp;amp;&amp;amp; !stall_load ) ) &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
        ! (( cc2_pending_valid &amp;amp;&amp;amp; ( ci_load_request_address.index == cc2_pending_address.index ) ) ||&lt;br /&gt;
             ( cc3_pending_valid &amp;amp;&amp;amp; ( ci_load_request_address.index == cc3_pending_address.index ) ))  &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
        ni_request_network_available;&lt;br /&gt;
&lt;br /&gt;
Response messages do not need feedbacks from MSHR since they do not allocate a new entry and they are never stalled. The same goes for flush requests even though they could be stalled by the relative stall protocol ROM.&lt;br /&gt;
&lt;br /&gt;
Finally a ''replacement'' request could be ''pre-allocated'' in MSHR (see [[L1 Cache Controller#MSHR Update Logic | MSHR update logic]]). In order for this request to be issued before every other request on the same block, an additional condition is added:&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_replacement = &lt;br /&gt;
 ...&lt;br /&gt;
 ( !replacement_mshr_hit ||&lt;br /&gt;
     ( replacement_mshr_hit &amp;amp;&amp;amp; !replacement_mshr.valid) ||&lt;br /&gt;
     ( replacement_mshr_hit &amp;amp;&amp;amp; replacement_mshr.valid &amp;amp;&amp;amp; ( !stall_replacement || replacement_mshr.waiting_for_eviction ) ) ) &lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Note the control logic does not check if the MSHR has free entries, we made the following assumption which eases this control: only a request per thread can be issued and only threads can allocate an MSHR entry, it is sufficient to size MSHR to the number of threads x 2 in order for the MSHR to be never full and make the control about his filling useless. In the worst case, the MSHR has a pending request and a pending replacement per thread.&lt;br /&gt;
&lt;br /&gt;
=== Requests Scheduler ===&lt;br /&gt;
Once the conditions for the issue have been verified, two or more requests could be ready at the same time so a scheduler must be used. Every request has a fixed priority whose order is set as below:&lt;br /&gt;
&lt;br /&gt;
# flush &lt;br /&gt;
# dinv&lt;br /&gt;
# replacement&lt;br /&gt;
# store miss&lt;br /&gt;
# coherence response &lt;br /&gt;
# coherence forwarded request&lt;br /&gt;
# load miss&lt;br /&gt;
# recycled response&lt;br /&gt;
&lt;br /&gt;
Once a type of request has been scheduled this block drives conveniently the output signals for the second stage.&lt;br /&gt;
&lt;br /&gt;
== Stage 2 ==&lt;br /&gt;
Stage 2 is responsible for managing L1 cache, the MSHR and forwarding signals from Stage 1 to Stage 3. It simply contains the L1 coherence cache (L1 data cache is in load/store unit) and all related logic for managing cache hits and block replacement. The policy used to replace a block is LRU (Least Recently Used). &amp;lt;br&amp;gt;&lt;br /&gt;
This module receives signals from stage 3 to update MSHR and coherence cache properly once a request is processed and from load/store unit to update LRU every time a block is accessed from the core. &lt;br /&gt;
&lt;br /&gt;
=== Hit/miss logic ===&lt;br /&gt;
&lt;br /&gt;
Lookup phase is split in two parts performed by:&lt;br /&gt;
&lt;br /&gt;
# load/store unit;&lt;br /&gt;
# cache controller (stage 2).&lt;br /&gt;
&lt;br /&gt;
Load/store unit performs the first lookup using only request's set; so it returns an array of tags (one per way) whose tags have the same set of the request and their privilege bits. This first lookup is performed at the same time the request is in cache controller stage 1. The second phase of lookup is performed by cache controller stage 2 using only the request's tag; this search is performed on the array provided by load/store unit. If there is a block with the same tag and the block is valid (its validity is checked with privilege bits) then a hit occurs and the way index of that block is provided to stage 3. The way index will be used by stage 3 to perform updates to coherence data of that block. &amp;lt;br&amp;gt;&lt;br /&gt;
If there is no block with the same tag as the request's and no hit occurs, stage 3 takes the way index provided by LRU unit in order to replace that block (see [[L1 Cache Controller#Replacement Logic | replacement logic]]).&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 // Second phase lookup&lt;br /&gt;
 // Result of this lookup is an array one-hot codified &lt;br /&gt;
 assign snoop_tag_way_oh[dcache_way] = ( ldst_snoop_tag[dcache_way] == cc1_request_address.tag ) &amp;amp; ( ldst_snoop_privileges[dcache_way].can_read | ldst_snoop_privileges[dcache_way].can_write );&lt;br /&gt;
 ...&lt;br /&gt;
 &lt;br /&gt;
 assign snoop_tag_hit       = |snoop_tag_way_oh;&lt;br /&gt;
&lt;br /&gt;
Note that whenever a request arrives in stage 2 its way index in the data cache is not known yet (since hit/miss logic is computing it at the same time), hence coherence cache is looked up only issuing on the bus the request's set. The result of the snoop operation is forwarded to stage 3, which elaborates them. Stage 3 knows which way index to use for fetching correct data because meanwhile hit/miss logic will have provided it.&lt;br /&gt;
&lt;br /&gt;
The choice of splitting lookup into two separate phases has been made in order to reduce the latency of the entire process.&lt;br /&gt;
&lt;br /&gt;
=== MSHR ===&lt;br /&gt;
''Miss Status Handling Register'' is used to handle cache lines data whose coherence transactions are pending; that is the case in which a cache block is in a non-stable state. Bear in mind that only one request per thread can be issued, MSHR has the same entry as the number of hardware threads.&lt;br /&gt;
&lt;br /&gt;
An MSHR entry comprises the following data:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Valid &lt;br /&gt;
! Address&lt;br /&gt;
! Thread ID &lt;br /&gt;
! Wakeup Thread &lt;br /&gt;
! State&lt;br /&gt;
! Waiting For Eviction  &lt;br /&gt;
! Ack Count &lt;br /&gt;
! Data &lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* Valid: entry has valid data&lt;br /&gt;
* Address: entry memory address&lt;br /&gt;
* Thread ID: requesting HW thread id&lt;br /&gt;
* Wakeup Thread: wakeup thread when the transaction is over &lt;br /&gt;
* State: actual coherence state&lt;br /&gt;
* Waiting for eviction: asserted for replacement requests&lt;br /&gt;
* Ack count: remaining acks to receive&lt;br /&gt;
* Data: data associated to request&lt;br /&gt;
&lt;br /&gt;
Note that entry's ''Data'' are stored in a separate SRAM memory in order to ease the lookup process.&lt;br /&gt;
&lt;br /&gt;
==== Implementation details ====&lt;br /&gt;
Since MSHR has to provide a lookup service to stage 1 (see [[L1 Cache Controller#MSHR Signals | lookup signals]]) and update entries coming from stage 3 (see [[L1 Cache Controller#MSHR Update Logic | update signals]]) at the '''same time''', dedicated read and a write ports have been implemented for this purpose. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Write port =====&lt;br /&gt;
A write policy is defined in order to define an order between writes and reads. This policy is can be set through a boolean parameter named WRITE_FIRST. &amp;lt;br&amp;gt;&lt;br /&gt;
In particular, this module is instantiated with policy WRITE_FIRST set to false, this means MSHR will serve read operations ''before'' write operations; write operations are '''delayed''' of one clock cycle after they have been issued from stage 3 (because a register delays the update). Here is the code regarding write port:&lt;br /&gt;
&lt;br /&gt;
 // This logic is generated for each MSHR entry&lt;br /&gt;
 generate&lt;br /&gt;
   genvar mshr_id;&lt;br /&gt;
     for ( mshr_id = 0; mshr_id &amp;lt; `MSHR_SIZE; mshr_id++ ) begin : mshr_entries&lt;br /&gt;
  &lt;br /&gt;
       ...&lt;br /&gt;
       // Write policy&lt;br /&gt;
       if (WRITE_FIRST == &amp;quot;TRUE&amp;quot;)&lt;br /&gt;
          // If true writes are serviced immediately&lt;br /&gt;
          assign data_updated[mshr_id] = (enable &amp;amp;&amp;amp; update_this_index) ? update_entry : data[mshr_id];&lt;br /&gt;
       else&lt;br /&gt;
          assign data_updated[mshr_id] = data[mshr_id];&lt;br /&gt;
 &lt;br /&gt;
       ...&lt;br /&gt;
       &lt;br /&gt;
       // Data entries (set of registers)&lt;br /&gt;
       always_ff @(posedge clk, posedge reset) begin&lt;br /&gt;
          if (reset)&lt;br /&gt;
             data[mshr_id] &amp;lt;= 0;&lt;br /&gt;
          else if (enable &amp;amp;&amp;amp; update_this_index)&lt;br /&gt;
             data[mshr_id] &amp;lt;= update_entry;&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
     end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
===== Read port =====&lt;br /&gt;
Read port implements a simple hit/miss logic for requests coming from stage 1 (see [[L1 Cache Controller#MSHR Signals | lookup signals]]). Write policy influents which data this logic will read though; if WRITE_FIRST is set to true then lookup is made on data just updated by write logic otherwise the lookup will be made before an update, the latter is the case when reads have more priority than writes (WRITE_FIRST is false). Here is the code regarding lookup logic:&lt;br /&gt;
&lt;br /&gt;
 // This logic is generated for each MSHR entry&lt;br /&gt;
 ...&lt;br /&gt;
 generate&lt;br /&gt;
   for ( i = 0; i &amp;lt; `MSHR_SIZE; i++ ) begin : lookup_logic&lt;br /&gt;
 &lt;br /&gt;
       // data_updated[] data are set according to write policy&lt;br /&gt;
       assign hit_map[i] = ( data_updated[i].address.index == index ) &amp;amp;&amp;amp; data_updated[i].valid;&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
  endgenerate&lt;br /&gt;
 ...&lt;br /&gt;
 &lt;br /&gt;
 assign hit        = |hit_map;&lt;br /&gt;
&lt;br /&gt;
== Stage 3 ==&lt;br /&gt;
Stage 3 is responsible for the actual execution of requests. Once a request is processed, this stage issues signals to the units in the above stages in order to update data properly. &amp;lt;br&amp;gt;&lt;br /&gt;
In particular, this stage drives datapath to perform one of these functions:&lt;br /&gt;
&lt;br /&gt;
* block replacement evaluation;&lt;br /&gt;
* MSHR update;&lt;br /&gt;
* cache memory (both data and coherence info) update.&lt;br /&gt;
* preparing outgoing coherence messages.&lt;br /&gt;
&lt;br /&gt;
=== Current State Selector ===&lt;br /&gt;
Before a request is processed by coherence protocol the correct source of cache block state has to be chosen. These data could be retrieved from:&lt;br /&gt;
&lt;br /&gt;
* MSHR;&lt;br /&gt;
* coherence data cache;&lt;br /&gt;
&lt;br /&gt;
If none of the conditions above are met then cache block must be in state '''I''' because it has not been ever read or modified.&lt;br /&gt;
&lt;br /&gt;
=== Protocol ROM ===&lt;br /&gt;
This module implements the coherence protocol as represented in the figure below. The choice to implement the protocol as a separate ROM has been made to ease further optimizations or changes to the protocol. It takes in input the current state and the request type and computes the next actions.&lt;br /&gt;
&lt;br /&gt;
[[File:MSI_Protocol_cc-rom_p1_new.png|1100px|MSI_CC]]&lt;br /&gt;
&lt;br /&gt;
[[File:MSI_Protocol_cc-rom_p2_new.png|1100px|MSI_CC]]&lt;br /&gt;
&lt;br /&gt;
The coherence protocol used is MSI plus some changes due to the directory's inclusivity. In particular, a new type of forwarded request has been added, ''recall'', that is sent by directory controller when a block has to be evicted from L2 cache. A ''writeback'' response to the memory controller follows in response to a ''recall'' only when the block is in state '''M'''. Note that a ''writeback'' response is sent to the directory controller as well in order to provide a sort of acknowledgement.&lt;br /&gt;
&lt;br /&gt;
Furthermore, another type of request, called ''flush'', has been added that simply send updated data to the from the requestor L1main memory. It also generates a ''writeback'' response even though it is directed only to the memory controller and does not impact on the coherence block state. Flushes are often used in applications for sending back to the main memory the output after the computation. &lt;br /&gt;
&lt;br /&gt;
The above table refers to a baseline protocol which explains the main logic behind the Protocol ROM. Further optimizations, such as the uncoherent states, are deeply described in detail in [[MSI Protocol]].&lt;br /&gt;
&lt;br /&gt;
=== Replacement Logic ===&lt;br /&gt;
A cache block replacement might occur whenever a new block has to be stored into the L1 and all the sets are busy. In case of available sets, the control logic will select them avoiding replacement. Hence, an eviction occurs only when the selected block has valid information. Block validity is assured by privilege bits associated with it. These privilege bits (one for each way) come from Stage 2 that in turn has received them from load/store unit. The pseudo-LRU module, in Stage 2, selects the block to replace pointing least used way.&lt;br /&gt;
&lt;br /&gt;
 replaced_way_valid                     = cc2_request_snoop_privileges[cc2_request_lru_way_idx].can_read | cc2_request_snoop_privileges[cc2_request_lru_way_idx].can_write;&lt;br /&gt;
&lt;br /&gt;
The address of the evicting block has to be reconstructed. In particular, its tag is provided by tag cache from load/store unit (through Stage 2) while the index is provided by the requesting address which will take its place in the cache (since the two addresses have the same set). In case of a dirty block, the data has to be fetched and send back to the main memory, stored into the data cache in Stage 2. The address offset is kept low since the eviction operation involves the entire block.&lt;br /&gt;
 &lt;br /&gt;
 replaced_way_address.tag               = cc2_request_snoop_tag[cc2_request_lru_way_idx];&lt;br /&gt;
 replaced_way_address.index             = cc2_request_address.index;&lt;br /&gt;
 replaced_way_address.offset            = {`DCACHE_OFFSET_LENGTH{1'b0}};&lt;br /&gt;
 &lt;br /&gt;
 replaced_way_state                     = cc2_request_coherence_states[cc2_request_lru_way_idx];&lt;br /&gt;
&lt;br /&gt;
Recapping, a replacement request is issued if:&lt;br /&gt;
&lt;br /&gt;
* protocol ROM requested for a cache update due to a new incoming data;&lt;br /&gt;
* the block requested is not present in the L1 cache (so the update request must be a block allocation);&lt;br /&gt;
* replaced block is valid.&lt;br /&gt;
&lt;br /&gt;
 do_replacement                        = pr_output.write_data_on_cache &amp;amp;&amp;amp; !cc2_request_snoop_hit &amp;amp;&amp;amp; replaced_way_valid;&lt;br /&gt;
&lt;br /&gt;
=== MSHR Update Logic ===&lt;br /&gt;
MSHR could be updated in three different ways:&lt;br /&gt;
&lt;br /&gt;
* entry allocation;&lt;br /&gt;
* entry deallocation;&lt;br /&gt;
* entry update.&lt;br /&gt;
&lt;br /&gt;
MSHR is used to store information on pending transactions. Whenever a cache line is in the MSHR it has a non-stable state, and the state stored in the MSHR is considered the most up-to-date. So a new entry allocation is made every time the cache line state turns into a non-stable state. On the other hand, deallocation of an entry is made when a cache line's state turns into a stable state and it was pending in the MSHR, this means that the ongoing transaction is over. Finally, an update is made when there is something to change regarding the stored information in the MSHR, and the cache line state is still non-stable, e.g. if the penguin transaction is waiting for acknowledges from all the sharers, whenever an ack message arrives it increases the total number of ack received (hence update this information in the MSHR), but the transaction is still ongoing until all ack messages have arrived. Each condition is represented by a signal that is properly asserted by protocol ROM.&lt;br /&gt;
&lt;br /&gt;
 cc3_update_mshr_en            = ( pr_output.allocate_mshr_entry || pr_output.update_mshr_entry || pr_output.deallocate_mshr_entry );&lt;br /&gt;
&lt;br /&gt;
Whenever the control signal &amp;lt;code&amp;gt;do_replacement&amp;lt;/code&amp;gt; is asserted an MSHR entry is ''pre-allocated''. This is necessary otherwise data computed by [[L1 Cache Controller#Replacement Logic|Replacement Logic]] could be lost. The Stage 1  checks if an entry is pre-allocated during the scheduling by reading the &amp;lt;code&amp;gt;waiting_for_eviction&amp;lt;/code&amp;gt; bit, see [[L1 Cache Controller#Request Issue Signals | Request Issue Signals]].&lt;br /&gt;
&lt;br /&gt;
Note that, an issued request from the Stage 1 allocates a new entry, the index of an empty entry is provided directly by MSHR (through Stage 2). Remember that, due our previous assumptions, there is surely an empty MSHR entry otherwise, the request would have not been issued (see [[L1 Cache Controller#Request Issue Signals | Request Issue Signals]]). If the operation is an update or deallocation then the index is obtained from Stage 1 querying the MSHR on the index of the entry associated with the actual request (see [[L1 Cache Controller#MSHR Signals|MSHR Signals]]). &lt;br /&gt;
&lt;br /&gt;
 cc3_update_mshr_index      = cc2_request_mshr_hit ? cc2_request_mshr_index : cc2_request_mshr_empty_index;&lt;br /&gt;
&lt;br /&gt;
=== Cache Update Logic ===&lt;br /&gt;
Both data cache and coherence cache could be updated after a coherence transaction has been computed. Data cache is updated according to the occurrence of a replacement, in that case, command &amp;lt;code&amp;gt;CC_REPLACEMENT&amp;lt;/code&amp;gt; is issued to load/store unit; this command ensures load/store unit will prepare the block for eviction. Otherwise, an update to cache block has to be made; if the update involves only privileges then &amp;lt;code&amp;gt;CC_UPDATE_INFO&amp;lt;/code&amp;gt; command is issued otherwise command &amp;lt;code&amp;gt;CC_UPDATE_INFO_DATA&amp;lt;/code&amp;gt; is issued when both the new block and its privileges are updated into the L1 cache.&lt;br /&gt;
&lt;br /&gt;
 // Data cache signals&lt;br /&gt;
 assign cc3_update_ldst_command          = do_replacement ? CC_REPLACEMENT : ( pr_output.write_data_on_cache ? CC_UPDATE_INFO_DATA : CC_UPDATE_INFO );&lt;br /&gt;
 assign cc3_update_ldst_way              = cc2_request_snoop_hit ? cc2_request_snoop_way_idx : cc2_request_lru_way_idx;&lt;br /&gt;
 ...&lt;br /&gt;
  &lt;br /&gt;
 // Coherence cache signals&lt;br /&gt;
 assign cc3_update_coherence_state_index = cc2_request_address.index;&lt;br /&gt;
 assign cc3_update_coherence_state_way   = cc2_request_snoop_hit ? cc2_request_snoop_way_idx : cc2_request_lru_way_idx;&lt;br /&gt;
 assign cc3_update_coherence_state_entry = pr_output.next_state;&lt;br /&gt;
&lt;br /&gt;
Data cache is updated whenever updating privileges for a block in the L1 is necessary, or whenever a new block is received and has to be stored in the cache along with its privileges.&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 cc3_update_ldst_valid         = ( pr_output.update_privileges &amp;amp;&amp;amp; cc2_request_snoop_hit ) || pr_output.write_data_on_cache;&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
The code above describes the condition of updating a line in the L1 cache. The cache is updated whenever block state became stable (its transaction is over and it has been deallocated from the MSHR) and there is a cache hit (it is already stored in the cache). Otherwise, whenever the coherence protocol requires the update of the cache, this is signalled through the &amp;lt;code&amp;gt;pr_output.write_data_on_cache&amp;lt;/code&amp;gt; bit, output of the protocol ROM.&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 cc3_update_coherence_state_en = ( pr_output.next_state_is_stable &amp;amp;&amp;amp; cc2_request_snoop_hit ) || ( pr_output.next_state_is_stable &amp;amp;&amp;amp; !cc2_request_snoop_hit &amp;amp;&amp;amp; pr_output.write_data_on_cache );&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Furthermore, if the request is a forwarded coherence request then L1 cache data are forwarded to the message generator in stage 4 in order to be sent to the requestor.&lt;br /&gt;
&lt;br /&gt;
 assign cc3_snoop_data_valid             = cc2_request_valid &amp;amp;&amp;amp; pr_output.send_data_from_cache;&lt;br /&gt;
 assign cc3_snoop_data_set               = cc2_request_address.index;&lt;br /&gt;
 assign cc3_snoop_data_way               = cc2_request_snoop_way_idx;&lt;br /&gt;
&lt;br /&gt;
== Stage 4 ==&lt;br /&gt;
This stage generates a correct request/response message for the network interface whenever a message is issued from the third stage.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
[[Coherence]]&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=L1_Cache_Controller&amp;diff=2263</id>
		<title>L1 Cache Controller</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=L1_Cache_Controller&amp;diff=2263"/>
				<updated>2019-07-01T13:16:49Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Cache controller manages the L1 cache. In particular, it handles only coherence information (such as states) since L1 data cache is managed by load/store unit.&lt;br /&gt;
&lt;br /&gt;
[[File:l1_cache.jpg|800px|LDST_CC]]&lt;br /&gt;
&lt;br /&gt;
The component is composed of 4 stages:&lt;br /&gt;
&lt;br /&gt;
* stage 1: schedules a pending request to issue (from local core or network);&lt;br /&gt;
* stage 2: contains coherence cache and MSHR;&lt;br /&gt;
* stage 3: processes a request properly with coherence protocol;&lt;br /&gt;
* stage 4: prepares coherence request/response to be sent on the network.&lt;br /&gt;
&lt;br /&gt;
All these stages are represented in the figure below. The component has been realized in a ''pipelined'' fashion in order for the controller to be able to serve multiple requests at the same time.&lt;br /&gt;
&lt;br /&gt;
=== Assumptions ===&lt;br /&gt;
The design has been driven by these assumptions:&lt;br /&gt;
&lt;br /&gt;
* cache controller schedules a request only when no other requests with on the same address are pending in the pipeline;&lt;br /&gt;
* coherence transactions are at the memory blocks level;&lt;br /&gt;
* cache controller does not schedule requests when another is in the pipeline and have the same set;&lt;br /&gt;
* only requests from the local core (load, store, replacement) allocate MSHR entries;&lt;br /&gt;
* information regarding cache block in a non-stable state are stored into the MSHR.&lt;br /&gt;
&lt;br /&gt;
Two requests on the same block cannot be issued one after another; when the first request is issued, it may modify an MSHR entry after two clock cycles (stage 3), hence the second request may read a non-up-to-date entry.&lt;br /&gt;
&lt;br /&gt;
== Stage 1 ==&lt;br /&gt;
Stage 1 is responsible for the scheduling of requests into the controller. A request could be a load miss, store miss, flush and replacement request from the local core or a coherence forwarded request or response from the network interface.&lt;br /&gt;
&lt;br /&gt;
=== MSHR Signals ===&lt;br /&gt;
The arbiter in the first stage checks if a pending request can be issued, in order to eligible for scheduling no other requests on the '''same block''' should be ''ongoing'' (or under elaboration) in the cache controller. Ongoing requests are stored in the MSHR table. Tags and sets are provided by the MSHR for each type of pending requests and are forwarded to the arbiter at Stage 1. The arbiter uses the information provided on ongoing transactions to select a pending request. The MSHR provides a look-up port for each type of request, a &amp;lt;code&amp;gt;hit&amp;lt;/code&amp;gt; single is provided along, the request is considered valid in the MSHR if such a signal is asserted: &lt;br /&gt;
&lt;br /&gt;
 // Signals to MSHR&lt;br /&gt;
 assign cc1_mshr_lookup_tag[MSHR_LOOKUP_PORT_LOAD ]   = ci_load_request_address.tag;&lt;br /&gt;
 assign cc1_mshr_lookup_set[MSHR_LOOKUP_PORT_LOAD ]   = ci_load_request_address.index;&lt;br /&gt;
 &lt;br /&gt;
 // Signals from MSHR&lt;br /&gt;
 assign load_mshr_hit                                 = cc2_mshr_lookup_hit[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
 assign load_mshr_index                               = cc2_mshr_lookup_index[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
 assign load_mshr                                     = cc2_mshr_lookup_entry_info[MSHR_LOOKUP_PORT_LOAD ];&lt;br /&gt;
&lt;br /&gt;
=== Stall Protocol ROMs ===&lt;br /&gt;
In order to be compliant with the coherence protocol, all incoming requests for blocks that are in a non-stable state have to be stalled. This task is performed through a series of protocol ROMs (one for each request type) that output state when the issue of relative request ought to be stalled, e.g. when a block is in state SM_A and a ''Fwd_GetS'', ''Fwd_GetM'', ''recall'', ''flush'', ''store'' or ''replacement'' request for the same block is received. In order to assert this signal, the protocol ROM needs the type of the request and the actual state of the block. The module &amp;lt;code&amp;gt;stall_protocol_rom&amp;lt;/code&amp;gt; implements this logic:&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom load_stall_protocol_rom (&lt;br /&gt;
   .current_request ( load            ),&lt;br /&gt;
   .current_state   ( load_mshr.state ),&lt;br /&gt;
   .pr_output_stall ( stall_load      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom store_stall_protocol_rom (&lt;br /&gt;
    .current_request ( store            ),&lt;br /&gt;
    .current_state   ( store_mshr.state ),&lt;br /&gt;
    .pr_output_stall ( stall_store      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom flush_stall_protocol_rom (&lt;br /&gt;
    .current_request ( flush            ),&lt;br /&gt;
    .current_state   ( flush_mshr.state ),&lt;br /&gt;
    .pr_output_stall ( stall_flush      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom replacement_stall_protocol_rom (&lt;br /&gt;
     .current_request ( replacement            ),&lt;br /&gt;
     .current_state   ( replacement_mshr.state ),&lt;br /&gt;
     .pr_output_stall ( stall_replacement      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
 stall_protocol_rom forwarded_stall_protocol_rom (&lt;br /&gt;
    .current_request ( fwd_2_creq( ni_forwarded_request.packet_type ) ),&lt;br /&gt;
    .current_state   ( forwarded_request_mshr.state                   ),&lt;br /&gt;
    .pr_output_stall ( stall_forwarded_request                        )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Note that response messages are never stalled in the coherence protocol, such requests are stalled only if a pending request with the same set index is already in the pipeline:&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_response                                      = ni_response_valid &amp;amp;&lt;br /&gt;
 	!(&lt;br /&gt;
 		( cc2_pending_valid &amp;amp;&amp;amp; ( ni_response.memory_address.index == cc2_pending_address.index ) ) ||&lt;br /&gt;
 		( cc3_pending_valid &amp;amp;&amp;amp; ( ni_response.memory_address.index == cc3_pending_address.index ) )&lt;br /&gt;
 	);&lt;br /&gt;
&lt;br /&gt;
=== Issuing a Request ===&lt;br /&gt;
In order to issue a generic request, it is required that:&lt;br /&gt;
&lt;br /&gt;
* MSHR has no pending requests for the same block;&lt;br /&gt;
* if the request is already in MSHR it has to be not valid;&lt;br /&gt;
* if the request is already in MSHR and valid it must not have been stalled by the protocol ROM (see [[L1 Cache Controller#Stall Protocol ROMs | stall signals]]).&lt;br /&gt;
* further stages are not serving a request on the same address (see [[L1 Cache Controller#Assumptions | assumptions]]);&lt;br /&gt;
* network interface is available;&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_load = ci_load_request_valid &amp;amp;&amp;amp; &lt;br /&gt;
 &lt;br /&gt;
        ( !load_mshr_hit || &lt;br /&gt;
             ( load_mshr_hit &amp;amp;&amp;amp; !load_mshr.valid) ||&lt;br /&gt;
              ( load_mshr_hit &amp;amp;&amp;amp; load_mshr.valid  &amp;amp;&amp;amp; load_mshr.address.tag == ci_load_request_address.tag &amp;amp;&amp;amp; !stall_load ) ) &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
        ! (( cc2_pending_valid &amp;amp;&amp;amp; ( ci_load_request_address.index == cc2_pending_address.index ) ) ||&lt;br /&gt;
             ( cc3_pending_valid &amp;amp;&amp;amp; ( ci_load_request_address.index == cc3_pending_address.index ) ))  &amp;amp;&amp;amp;&lt;br /&gt;
 &lt;br /&gt;
        ni_request_network_available;&lt;br /&gt;
&lt;br /&gt;
Response messages do not need feedbacks from MSHR since they do not allocate a new entry and they are never stalled. The same goes for flush requests even though they could be stalled by the relative stall protocol ROM.&lt;br /&gt;
&lt;br /&gt;
Finally a ''replacement'' request could be ''pre-allocated'' in MSHR (see [[L1 Cache Controller#MSHR Update Logic | MSHR update logic]]). In order for this request to be issued before every other request on the same block, an additional condition is added:&lt;br /&gt;
&lt;br /&gt;
 assign can_issue_replacement = &lt;br /&gt;
 ...&lt;br /&gt;
 ( !replacement_mshr_hit ||&lt;br /&gt;
     ( replacement_mshr_hit &amp;amp;&amp;amp; !replacement_mshr.valid) ||&lt;br /&gt;
     ( replacement_mshr_hit &amp;amp;&amp;amp; replacement_mshr.valid &amp;amp;&amp;amp; ( !stall_replacement || replacement_mshr.waiting_for_eviction ) ) ) &lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Note the control logic does not check if the MSHR has free entries, we made the following assumption which eases this control: only a request per thread can be issued and only threads can allocate an MSHR entry, it is sufficient to size MSHR to the number of threads x 2 in order for the MSHR to be never full and make the control about his filling useless. In the worst case, the MSHR has a pending request and a pending replacement per thread.&lt;br /&gt;
&lt;br /&gt;
=== Requests Scheduler ===&lt;br /&gt;
Once the conditions for the issue have been verified, two or more requests could be ready at the same time so a scheduler must be used. Every request has a fixed priority whose order is set as below:&lt;br /&gt;
&lt;br /&gt;
# flush &lt;br /&gt;
# dinv&lt;br /&gt;
# replacement&lt;br /&gt;
# store miss&lt;br /&gt;
# coherence response &lt;br /&gt;
# coherence forwarded request&lt;br /&gt;
# load miss&lt;br /&gt;
# recycled response&lt;br /&gt;
&lt;br /&gt;
Once a type of request has been scheduled this block drives conveniently the output signals for the second stage.&lt;br /&gt;
&lt;br /&gt;
== Stage 2 ==&lt;br /&gt;
Stage 2 is responsible for managing L1 cache, the MSHR and forwarding signals from Stage 1 to Stage 3. It simply contains the L1 coherence cache (L1 data cache is in load/store unit) and all related logic for managing cache hits and block replacement. The policy used to replace a block is LRU (Least Recently Used). &amp;lt;br&amp;gt;&lt;br /&gt;
This module receives signals from stage 3 to update MSHR and coherence cache properly once a request is processed and from load/store unit to update LRU every time a block is accessed from the core. &lt;br /&gt;
&lt;br /&gt;
=== Hit/miss logic ===&lt;br /&gt;
&lt;br /&gt;
Lookup phase is split in two parts performed by:&lt;br /&gt;
&lt;br /&gt;
# load/store unit;&lt;br /&gt;
# cache controller (stage 2).&lt;br /&gt;
&lt;br /&gt;
Load/store unit performs the first lookup using only request's set; so it returns an array of tags (one per way) whose tags have the same set of the request and their privilege bits. This first lookup is performed at the same time the request is in cache controller stage 1. The second phase of lookup is performed by cache controller stage 2 using only the request's tag; this search is performed on the array provided by load/store unit. If there is a block with the same tag and the block is valid (its validity is checked with privilege bits) then a hit occurs and the way index of that block is provided to stage 3. The way index will be used by stage 3 to perform updates to coherence data of that block. &amp;lt;br&amp;gt;&lt;br /&gt;
If there is no block with the same tag as the request's and no hit occurs, stage 3 takes the way index provided by LRU unit in order to replace that block (see [[L1 Cache Controller#Replacement Logic | replacement logic]]).&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 // Second phase lookup&lt;br /&gt;
 // Result of this lookup is an array one-hot codified &lt;br /&gt;
 assign snoop_tag_way_oh[dcache_way] = ( ldst_snoop_tag[dcache_way] == cc1_request_address.tag ) &amp;amp; ( ldst_snoop_privileges[dcache_way].can_read | ldst_snoop_privileges[dcache_way].can_write );&lt;br /&gt;
 ...&lt;br /&gt;
 &lt;br /&gt;
 assign snoop_tag_hit       = |snoop_tag_way_oh;&lt;br /&gt;
&lt;br /&gt;
Note that whenever a request arrives in stage 2 its way index in the data cache is not known yet (since hit/miss logic is computing it at the same time), hence coherence cache is looked up only issuing on the bus the request's set. The result of the snoop operation is forwarded to stage 3, which elaborates them. Stage 3 knows which way index to use for fetching correct data because meanwhile hit/miss logic will have provided it.&lt;br /&gt;
&lt;br /&gt;
The choice of splitting lookup into two separate phases has been made in order to reduce the latency of the entire process.&lt;br /&gt;
&lt;br /&gt;
=== MSHR ===&lt;br /&gt;
''Miss Status Handling Register'' is used to handle cache lines data whose coherence transactions are pending; that is the case in which a cache block is in a non-stable state. Bear in mind that only one request per thread can be issued, MSHR has the same entry as the number of hardware threads.&lt;br /&gt;
&lt;br /&gt;
An MSHR entry comprises the following data:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Valid &lt;br /&gt;
! Address&lt;br /&gt;
! Thread ID &lt;br /&gt;
! Wakeup Thread &lt;br /&gt;
! State&lt;br /&gt;
! Waiting For Eviction  &lt;br /&gt;
! Ack Count &lt;br /&gt;
! Data &lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* Valid: entry has valid data&lt;br /&gt;
* Address: entry memory address&lt;br /&gt;
* Thread ID: requesting HW thread id&lt;br /&gt;
* Wakeup Thread: wakeup thread when the transaction is over &lt;br /&gt;
* State: actual coherence state&lt;br /&gt;
* Waiting for eviction: asserted for replacement requests&lt;br /&gt;
* Ack count: remaining acks to receive&lt;br /&gt;
* Data: data associated to request&lt;br /&gt;
&lt;br /&gt;
Note that entry's ''Data'' are stored in a separate SRAM memory in order to ease the lookup process.&lt;br /&gt;
&lt;br /&gt;
==== Implementation details ====&lt;br /&gt;
Since MSHR has to provide a lookup service to stage 1 (see [[L1 Cache Controller#MSHR Signals | lookup signals]]) and update entries coming from stage 3 (see [[L1 Cache Controller#MSHR Update Logic | update signals]]) at the '''same time''', dedicated read and a write ports have been implemented for this purpose. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Write port =====&lt;br /&gt;
A write policy is defined in order to define an order between writes and reads. This policy is can be set through a boolean parameter named WRITE_FIRST. &amp;lt;br&amp;gt;&lt;br /&gt;
In particular, this module is instantiated with policy WRITE_FIRST set to false, this means MSHR will serve read operations ''before'' write operations; write operations are '''delayed''' of one clock cycle after they have been issued from stage 3 (because a register delays the update). Here is the code regarding write port:&lt;br /&gt;
&lt;br /&gt;
 // This logic is generated for each MSHR entry&lt;br /&gt;
 generate&lt;br /&gt;
   genvar mshr_id;&lt;br /&gt;
     for ( mshr_id = 0; mshr_id &amp;lt; `MSHR_SIZE; mshr_id++ ) begin : mshr_entries&lt;br /&gt;
  &lt;br /&gt;
       ...&lt;br /&gt;
       // Write policy&lt;br /&gt;
       if (WRITE_FIRST == &amp;quot;TRUE&amp;quot;)&lt;br /&gt;
          // If true writes are serviced immediately&lt;br /&gt;
          assign data_updated[mshr_id] = (enable &amp;amp;&amp;amp; update_this_index) ? update_entry : data[mshr_id];&lt;br /&gt;
       else&lt;br /&gt;
          assign data_updated[mshr_id] = data[mshr_id];&lt;br /&gt;
 &lt;br /&gt;
       ...&lt;br /&gt;
       &lt;br /&gt;
       // Data entries (set of registers)&lt;br /&gt;
       always_ff @(posedge clk, posedge reset) begin&lt;br /&gt;
          if (reset)&lt;br /&gt;
             data[mshr_id] &amp;lt;= 0;&lt;br /&gt;
          else if (enable &amp;amp;&amp;amp; update_this_index)&lt;br /&gt;
             data[mshr_id] &amp;lt;= update_entry;&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
     end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
===== Read port =====&lt;br /&gt;
Read port implements a simple hit/miss logic for requests coming from stage 1 (see [[L1 Cache Controller#MSHR Signals | lookup signals]]). Write policy influents which data this logic will read though; if WRITE_FIRST is set to true then lookup is made on data just updated by write logic otherwise the lookup will be made before an update, the latter is the case when reads have more priority than writes (WRITE_FIRST is false). Here is the code regarding lookup logic:&lt;br /&gt;
&lt;br /&gt;
 // This logic is generated for each MSHR entry&lt;br /&gt;
 ...&lt;br /&gt;
 generate&lt;br /&gt;
   for ( i = 0; i &amp;lt; `MSHR_SIZE; i++ ) begin : lookup_logic&lt;br /&gt;
 &lt;br /&gt;
       // data_updated[] data are set according to write policy&lt;br /&gt;
       assign hit_map[i] = ( data_updated[i].address.index == index ) &amp;amp;&amp;amp; data_updated[i].valid;&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
  endgenerate&lt;br /&gt;
 ...&lt;br /&gt;
 &lt;br /&gt;
 assign hit        = |hit_map;&lt;br /&gt;
&lt;br /&gt;
== Stage 3 ==&lt;br /&gt;
Stage 3 is responsible for the actual execution of requests. Once a request is processed, this stage issues signals to the units in the above stages in order to update data properly. &amp;lt;br&amp;gt;&lt;br /&gt;
In particular, this stage drives datapath to perform one of these functions:&lt;br /&gt;
&lt;br /&gt;
* block replacement evaluation;&lt;br /&gt;
* MSHR update;&lt;br /&gt;
* cache memory (both data and coherence info) update.&lt;br /&gt;
* preparing outgoing coherence messages.&lt;br /&gt;
&lt;br /&gt;
=== Current State Selector ===&lt;br /&gt;
Before a request is processed by coherence protocol the correct source of cache block state has to be chosen. These data could be retrieved from:&lt;br /&gt;
&lt;br /&gt;
* MSHR;&lt;br /&gt;
* coherence data cache;&lt;br /&gt;
&lt;br /&gt;
If none of the conditions above are met then cache block must be in state '''I''' because it has not been ever read or modified.&lt;br /&gt;
&lt;br /&gt;
=== Protocol ROM ===&lt;br /&gt;
This module implements the coherence protocol as represented in the figure below. The choice to implement the protocol as a separate ROM has been made to ease further optimizations or changes to the protocol. It takes in input the current state and the request type and computes the next actions.&lt;br /&gt;
&lt;br /&gt;
[[File:MSI_Protocol_cc-rom_p1_new.png|1100px|MSI_CC]]&lt;br /&gt;
&lt;br /&gt;
[[File:MSI_Protocol_cc-rom_p2_new.png|1100px|MSI_CC]]&lt;br /&gt;
&lt;br /&gt;
The coherence protocol used is MSI plus some changes due to the directory's inclusivity. In particular, a new type of forwarded request has been added, ''recall'', that is sent by directory controller when a block has to be evicted from L2 cache. A ''writeback'' response to the memory controller follows in response to a ''recall'' only when the block is in state '''M'''. Note that a ''writeback'' response is sent to the directory controller as well in order to provide a sort of acknowledgement.&lt;br /&gt;
&lt;br /&gt;
Furthermore, another type of request, called ''flush'', has been added that simply send updated data to the from the requestor L1main memory. It also generates a ''writeback'' response even though it is directed only to the memory controller and does not impact on the coherence block state. Flushes are often used in applications for sending back to the main memory the output after the computation. &lt;br /&gt;
&lt;br /&gt;
The above table refers to a baseline protocol which explains the main logic behind the Protocol ROM. Further optimizations, such as the uncoherent states, are deeply described in detail in [[MSI Protocol]].&lt;br /&gt;
&lt;br /&gt;
=== Replacement Logic ===&lt;br /&gt;
A cache block replacement might occur whenever a new block has to be stored into the L1 and all the sets are busy. In case of available sets, the control logic will select them avoiding replacement. Hence, an eviction occurs only when the selected block has valid information. Block validity is assured by privilege bits associated with it. These privilege bits (one for each way) come from Stage 2 that in turn has received them from load/store unit. The pseudo-LRU module, in Stage 2, selects the block to replace pointing least used way.&lt;br /&gt;
&lt;br /&gt;
 replaced_way_valid                     = cc2_request_snoop_privileges[cc2_request_lru_way_idx].can_read | cc2_request_snoop_privileges[cc2_request_lru_way_idx].can_write;&lt;br /&gt;
&lt;br /&gt;
The address of the evicting block has to be reconstructed. In particular, its tag is provided by tag cache from load/store unit (through Stage 2) while the index is provided by the requesting address which will take its place in the cache (since the two addresses have the same set). In case of a dirty block, the data has to be fetched and send back to the main memory, stored into the data cache in Stage 2. The address offset is kept low since the eviction operation involves the entire block.&lt;br /&gt;
 &lt;br /&gt;
 replaced_way_address.tag               = cc2_request_snoop_tag[cc2_request_lru_way_idx];&lt;br /&gt;
 replaced_way_address.index             = cc2_request_address.index;&lt;br /&gt;
 replaced_way_address.offset            = {`DCACHE_OFFSET_LENGTH{1'b0}};&lt;br /&gt;
 &lt;br /&gt;
 replaced_way_state                     = cc2_request_coherence_states[cc2_request_lru_way_idx];&lt;br /&gt;
&lt;br /&gt;
Recapping, a replacement request is issued if:&lt;br /&gt;
&lt;br /&gt;
* protocol ROM requested for a cache update due to a new incoming data;&lt;br /&gt;
* the block requested is not present in the L1 cache (so the update request must be a block allocation);&lt;br /&gt;
* replaced block is valid.&lt;br /&gt;
&lt;br /&gt;
 do_replacement                        = pr_output.write_data_on_cache &amp;amp;&amp;amp; !cc2_request_snoop_hit &amp;amp;&amp;amp; replaced_way_valid;&lt;br /&gt;
&lt;br /&gt;
=== MSHR Update Logic ===&lt;br /&gt;
MSHR could be updated in three different ways:&lt;br /&gt;
&lt;br /&gt;
* entry allocation;&lt;br /&gt;
* entry deallocation;&lt;br /&gt;
* entry update.&lt;br /&gt;
&lt;br /&gt;
MSHR is used to store information on pending transactions. Whenever a cache line is in the MSHR it has a non-stable state, and the state stored in the MSHR is considered the most up-to-date. So a new entry allocation is made every time the cache line state turns into a non-stable state. On the other hand, deallocation of an entry is made when a cache line's state turns into a stable state and it was pending in the MSHR, this means that the ongoing transaction is over. Finally, an update is made when there is something to change regarding the stored information in the MSHR, and the cache line state is still non-stable, e.g. if the penguin transaction is waiting for acknowledges from all the sharers, whenever an ack message arrives it increases the total number of ack received (hence update this information in the MSHR), but the transaction is still ongoing until all ack messages have arrived. Each condition is represented by a signal that is properly asserted by protocol ROM.&lt;br /&gt;
&lt;br /&gt;
 cc3_update_mshr_en            = ( pr_output.allocate_mshr_entry || pr_output.update_mshr_entry || pr_output.deallocate_mshr_entry );&lt;br /&gt;
&lt;br /&gt;
Whenever the control signal &amp;lt;code&amp;gt;do_replacement&amp;lt;/code&amp;gt; is asserted an MSHR entry is ''pre-allocated''. This is necessary otherwise data computed by [[L1 Cache Controller#Replacement Logic|Replacement Logic]] could be lost. The Stage 1  checks if an entry is pre-allocated during the scheduling by reading the &amp;lt;code&amp;gt;waiting_for_eviction&amp;lt;/code&amp;gt; bit, see [[L1 Cache Controller#Request Issue Signals | Request Issue Signals]].&lt;br /&gt;
&lt;br /&gt;
Note that, an issued request from the Stage 1 allocates a new entry, the index of an empty entry is provided directly by MSHR (through Stage 2). Remember that, due our previous assumptions, there is surely an empty MSHR entry otherwise, the request would have not been issued (see [[L1 Cache Controller#Request Issue Signals | Request Issue Signals]]). If the operation is an update or deallocation then the index is obtained from Stage 1 querying the MSHR on the index of the entry associated with the actual request (see [[L1 Cache Controller#MSHR Signals|MSHR Signals]]). &lt;br /&gt;
&lt;br /&gt;
 cc3_update_mshr_index      = cc2_request_mshr_hit ? cc2_request_mshr_index : cc2_request_mshr_empty_index;&lt;br /&gt;
&lt;br /&gt;
=== Cache Update Logic ===&lt;br /&gt;
Both data cache and coherence cache could be updated after a coherence transaction has been computed. Data cache is updated according to the occurrence of a replacement, in that case, command &amp;lt;code&amp;gt;CC_REPLACEMENT&amp;lt;/code&amp;gt; is issued to load/store unit; this command ensures load/store unit will prepare the block for eviction. Otherwise, an update to cache block has to be made; if the update involves only privileges then &amp;lt;code&amp;gt;CC_UPDATE_INFO&amp;lt;/code&amp;gt; command is issued otherwise command &amp;lt;code&amp;gt;CC_UPDATE_INFO_DATA&amp;lt;/code&amp;gt; is issued when both the new block and its privileges are updated into the L1 cache.&lt;br /&gt;
&lt;br /&gt;
 // Data cache signals&lt;br /&gt;
 assign cc3_update_ldst_command          = do_replacement ? CC_REPLACEMENT : ( pr_output.write_data_on_cache ? CC_UPDATE_INFO_DATA : CC_UPDATE_INFO );&lt;br /&gt;
 assign cc3_update_ldst_way              = cc2_request_snoop_hit ? cc2_request_snoop_way_idx : cc2_request_lru_way_idx;&lt;br /&gt;
 ...&lt;br /&gt;
  &lt;br /&gt;
 // Coherence cache signals&lt;br /&gt;
 assign cc3_update_coherence_state_index = cc2_request_address.index;&lt;br /&gt;
 assign cc3_update_coherence_state_way   = cc2_request_snoop_hit ? cc2_request_snoop_way_idx : cc2_request_lru_way_idx;&lt;br /&gt;
 assign cc3_update_coherence_state_entry = pr_output.next_state;&lt;br /&gt;
&lt;br /&gt;
Data cache is updated whenever updating privileges for a block in the L1 is necessary, or whenever a new block is received and has to be stored in the cache along with its privileges.&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 cc3_update_ldst_valid         = ( pr_output.update_privileges &amp;amp;&amp;amp; cc2_request_snoop_hit ) || pr_output.write_data_on_cache;&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
The code above describes the condition of updating a line in the L1 cache. The cache is updated whenever block state became stable (its transaction is over and it has been deallocated from the MSHR) and there is a cache hit (it is already stored in the cache). Otherwise, whenever the coherence protocol requires the update of the cache, this is signalled through the &amp;lt;code&amp;gt;pr_output.write_data_on_cache&amp;lt;/code&amp;gt; bit, output of the protocol ROM.&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 cc3_update_coherence_state_en = ( pr_output.next_state_is_stable &amp;amp;&amp;amp; cc2_request_snoop_hit ) || ( pr_output.next_state_is_stable &amp;amp;&amp;amp; !cc2_request_snoop_hit &amp;amp;&amp;amp; pr_output.write_data_on_cache );&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Furthermore, if the request is a forwarded coherence request then L1 cache data are forwarded to the message generator in stage 4 in order to be sent to the requestor.&lt;br /&gt;
&lt;br /&gt;
 assign cc3_snoop_data_valid             = cc2_request_valid &amp;amp;&amp;amp; pr_output.send_data_from_cache;&lt;br /&gt;
 assign cc3_snoop_data_set               = cc2_request_address.index;&lt;br /&gt;
 assign cc3_snoop_data_way               = cc2_request_snoop_way_idx;&lt;br /&gt;
&lt;br /&gt;
== Stage 4 ==&lt;br /&gt;
This stage generates a correct request/response message for the network interface whenever a message is issued from the third stage.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
[[Coherence]]&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Include&amp;diff=2262</id>
		<title>Include</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Include&amp;diff=2262"/>
				<updated>2019-07-01T11:20:09Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Network Defines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== NPU Defines == &lt;br /&gt;
Main NPU core defines are declared in the &amp;lt;code&amp;gt;npu_defines.sv&amp;lt;/code&amp;gt; file which stores global define at the core level. Parameters, such as the number of hardware lanes, number of register per register file, or memory address width, are defined in this file:&lt;br /&gt;
&lt;br /&gt;
 `define HW_LANE                 16&lt;br /&gt;
 `define ADDRESS_SIZE            32&lt;br /&gt;
 `define REGISTER_NUMBER         64&lt;br /&gt;
&lt;br /&gt;
along with information on special purpose registers: &lt;br /&gt;
&lt;br /&gt;
 `define PC_REG                  ( `REGISTER_NUMBER - 1 )&lt;br /&gt;
 `define RA_REG                  ( `REGISTER_NUMBER - 2 )&lt;br /&gt;
 `define SP_REG                  ( `REGISTER_NUMBER - 3 )&lt;br /&gt;
 `define FP_REG                  ( `REGISTER_NUMBER - 4 )&lt;br /&gt;
 `define MASK_REG                ( `REGISTER_NUMBER - 5 )&lt;br /&gt;
&lt;br /&gt;
and opcode definitions and instruction decoded-related data structure definitions:&lt;br /&gt;
&lt;br /&gt;
 NOT      = `OP_CODE_WIDTH'b000000,&lt;br /&gt;
 OR       = `OP_CODE_WIDTH'b000001,&lt;br /&gt;
 AND      = `OP_CODE_WIDTH'b000010,&lt;br /&gt;
 XOR      = `OP_CODE_WIDTH'b000011,&lt;br /&gt;
&lt;br /&gt;
== User Defines == &lt;br /&gt;
User defines are included into the file &amp;lt;code&amp;gt;npu_user_defines.sv&amp;lt;/code&amp;gt; which exposes to the final user all the configurable parameters of the architecture. Typically those parameters are bound to be a power of two.&lt;br /&gt;
&lt;br /&gt;
'''Core-side user defines''': all the following are bound to be a power of two&lt;br /&gt;
&lt;br /&gt;
* THREAD_NUMB: number of hardware thread instantiated, default 8.&lt;br /&gt;
* USER_ICACHE_SET: number of instruction cache sets, default 32.&lt;br /&gt;
* USER_ICACHE_WAY: number of instruction cache sets, default 4.&lt;br /&gt;
* USER_DCACHE_SET: number of data cache sets, default 32.&lt;br /&gt;
* USER_DCACHE_WAY: number of data cache sets, default 4.&lt;br /&gt;
* USER_L2CACHE_SET: number of L2 data cache sets, default 128.&lt;br /&gt;
* USER_L2CACHE_WAY: number of L2 data cache sets, default 8. &lt;br /&gt;
* NPU_SPM: when defined allocates a scratchpad memory in each NPU core.&lt;br /&gt;
* NPU_FPU: when defined allocates an FPU in each NPU core.&lt;br /&gt;
&lt;br /&gt;
'''System-side user defines''': &lt;br /&gt;
&lt;br /&gt;
* DIRECTORY_BARRIER: when defined the manycore system supports a distributed directory mechanism spread over all tiles. Otherwise, it allocates a single centralized directory. The single-core version always has a centralized synchronization master.&lt;br /&gt;
* CENTRAL_SYNCH_ID: Centralized directory ID, used only when DIRECTORY_BARRIER is undefined.&lt;br /&gt;
* NoC_X_WIDTH: Network-on-Chip mesh x dimension width, default value 2, must be a power of 2.&lt;br /&gt;
* NoC_Y_WIDTH: Network-on-Chip mesh x dimension width, default value 2, must be a 3power of 2.&lt;br /&gt;
* TILE_MEMORY_ID: Memory Tile ID.&lt;br /&gt;
* TILE_H2C_ID: Host interface Tile ID.&lt;br /&gt;
* TILE_NPU: number of tile with an NPU core.&lt;br /&gt;
* IO_MAP_BASE_ADDR: base address of the non-coherent memory space dedicated for IO devices, default value 0xFF00_0000.&lt;br /&gt;
* IO_MAP_SIZE: width of the non-coherent memory space dedicated for IO devices, default value 0x00FF_FF00.&lt;br /&gt;
&lt;br /&gt;
Furthermore, DISPLAY variables are defined, all commented by default. When a DISPLAY variable is active, it generates a file, under a folder named after the selected kernel. Each DISPLAY variable logs a defined kind of transaction, namely:&lt;br /&gt;
* DISPLAY_MEMORY: logs on file the memory state at the end of the kernel execution.&lt;br /&gt;
* DISPLAY_MEMORY_TRANS: logs on file all requests to the main memory.&lt;br /&gt;
* DISPLAY_MEMORY_CONTROLLER: displays on shell memory requests to the memory controller and its responses. &lt;br /&gt;
* DISPLAY_INT: logs every integer operation in the integer pipeline, and their results.&lt;br /&gt;
* DISPLAY_CORE: enables log from the core (file display_core.txt).&lt;br /&gt;
* DISPLAY_ISSUE: logs all scheduled instructions, and tracks the scheduled PC and the issued Thread, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_INT: logs all results from the integer module, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_WB: logs all results from the writeback module, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_LDST: enables logging into the load/store unit (file display_ldst.txt).&lt;br /&gt;
* DISPLAY_CACHE_CONTROLLER: logs memory transactions between Load/Store unit and the main memory.&lt;br /&gt;
* DISPLAY_SYNCH_CORE: logs synchronization requests within the core.&lt;br /&gt;
* DISPLAY_BARRIER_CORE: logs synchronization releases from the Synchronization master.&lt;br /&gt;
* DISPLAY_COHERENCE: logs all coherence transactions among CCs, DCs and MC.&lt;br /&gt;
* DISPLAY_THREAD_STATUS: displays all active threads status and trap reason.&lt;br /&gt;
&lt;br /&gt;
These variables selectively enable the logging of a specific feature. For each define, a log file is typically created in &amp;lt;code&amp;gt;npu/simulationlog/&amp;lt;name_of_the_kernel&amp;gt;/display_&amp;lt;name&amp;gt;&amp;lt;/code&amp;gt;. DISPLAY_SIMULATION_LOG variable has to be always defined in the simulation flow, this also displays architectural information on the tcl shell.&lt;br /&gt;
&lt;br /&gt;
== Scratchpad Memory Defines ==&lt;br /&gt;
&lt;br /&gt;
Defines related to the configuration of the scratchpad memory are stored into the &amp;lt;code&amp;gt;npu_spm_defines.sv&amp;lt;/code&amp;gt; file, which exposes to the final user all the configurable parameters related to the scratchpad memory at the core-level, along with other component-specific typedefs. The SPM has the following configurable parameters: &lt;br /&gt;
&lt;br /&gt;
* SM_PROCESSING_ELEMENTS: number of concurrent input requests, default value 16 (equal to the number of hardware lanes).&lt;br /&gt;
* SM_ENTRIES: number of entries per bank (similar to cache sets). &lt;br /&gt;
* SM_MEMORY_BANKS: number of banks, default value 16.&lt;br /&gt;
* SM_BYTE_PER_ENTRY: number of bytes per entry, default value 4.&lt;br /&gt;
&lt;br /&gt;
== Network Defines ==&lt;br /&gt;
&lt;br /&gt;
Network related defines are spread over two include files, namely &amp;lt;code&amp;gt;npu_message_service_defines.sv&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;npu_network_define.sv&amp;lt;/code&amp;gt;. The first defines shared typedefs used for the host interfacing mechanism over the Network-on-Chip. &lt;br /&gt;
The latter defines the length of flits and exposes two configuration parameters two the final user:&lt;br /&gt;
* VC_PER_PORT: number of Virtual Channel per router port (5 ports), default value 4, must be a power of 2.&lt;br /&gt;
* QUEUE_LEN_PER_VC: length of the FIFO for each Virtual Channel, default value 16, must be a power of 2.&lt;br /&gt;
&lt;br /&gt;
The following code snippet shows structures that ease flit management:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     flit_type_t flit_type;&lt;br /&gt;
     vc_id_t vc_id;&lt;br /&gt;
     port_t next_hop_port;&lt;br /&gt;
     tile_address_t destination;&lt;br /&gt;
     tile_destination_t core_destination;&lt;br /&gt;
 } flit_header_t;&lt;br /&gt;
&lt;br /&gt;
 typedef logic [`PAYLOAD_W-1:0] flit_body_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     flit_header_t header;&lt;br /&gt;
     flit_body_t payload;&lt;br /&gt;
 } flit_t;&lt;br /&gt;
&lt;br /&gt;
== Synchronization Defines ==&lt;br /&gt;
&lt;br /&gt;
Synchronization related structures and types are declared in the &amp;lt;code&amp;gt;npu_sychronization_defines.sv&amp;lt;/code&amp;gt; file, which mainly defines a data structure that describes the synchronization message at the highest level of the network stack:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     barrier_t id_barrier;&lt;br /&gt;
     cnt_barrier_t cnt_setup;&lt;br /&gt;
     tile_id_t tile_id_source;&lt;br /&gt;
 } sync_account_message_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     barrier_t id_barrier;&lt;br /&gt;
     logic [$bits(cnt_barrier_t)+$bits(tile_id_t)-1:0] padding;&lt;br /&gt;
 } sync_release_message_t;&lt;br /&gt;
&lt;br /&gt;
Synchronization traffics are encapsulated in a sync_message_t and then in a service_message_t:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     sync_message_type_t sync_type;&lt;br /&gt;
&lt;br /&gt;
     union packed {&lt;br /&gt;
          sync_account_message_t account_mess;&lt;br /&gt;
          sync_release_message_t release_mess;&lt;br /&gt;
     } sync_mess;&lt;br /&gt;
 } sync_message_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed{&lt;br /&gt;
     cnt_barrier_t cnt;&lt;br /&gt;
     tile_mask_t mask_slave;&lt;br /&gt;
 } barrier_data_t;&lt;br /&gt;
&lt;br /&gt;
== Coherence Defines == &lt;br /&gt;
&lt;br /&gt;
Coherence related defines are stored into &amp;lt;code&amp;gt;npu_coherence_defines.sv&amp;lt;/code&amp;gt; which declares all types and structures used by coherence actors (mainly '''Cache Controller''' and '''Directory Controller'''), such as TSHR entry type&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     logic                                  valid;&lt;br /&gt;
     logic [`DIRECTORY_STATE_WIDTH - 1 : 0] state;&lt;br /&gt;
     l2_cache_address_t                     address;&lt;br /&gt;
     tile_mask_t                            sharers_list;&lt;br /&gt;
     tile_address_t                         owner;&lt;br /&gt;
 } tshr_entry_t;&lt;br /&gt;
&lt;br /&gt;
or the request a CC can accept:&lt;br /&gt;
&lt;br /&gt;
 typedef enum coherence_request_t {&lt;br /&gt;
     load                   = 0,&lt;br /&gt;
     store                  = 1,&lt;br /&gt;
     replacement            = 2,&lt;br /&gt;
     Fwd_GetS               = 3,&lt;br /&gt;
     Fwd_GetM               = 4,&lt;br /&gt;
     Inv                    = 5,&lt;br /&gt;
     Put_Ack                = 6,&lt;br /&gt;
     Data_from_Dir_ack_eqz  = 7,&lt;br /&gt;
     Data_from_Dir_ack_gtz  = 8,&lt;br /&gt;
     Data_from_Owner        = 9,&lt;br /&gt;
     Inv_Ack                = 10,&lt;br /&gt;
     Last_Inv_Ack           = 11,&lt;br /&gt;
     recall                 = 12,&lt;br /&gt;
     flush                  = 13,&lt;br /&gt;
     load_uncoherent        = 14,&lt;br /&gt;
     store_uncoherent       = 15,&lt;br /&gt;
     replacement_uncoherent = 16,&lt;br /&gt;
     flush_uncoherent       = 17,&lt;br /&gt;
     Fwd_Flush              = 18,&lt;br /&gt;
     dinv                   = 19,&lt;br /&gt;
     dinv_uncoherent        = 20&lt;br /&gt;
 } coherence_requests_enum_t;&lt;br /&gt;
&lt;br /&gt;
and, of course, kind of message a coherence actor can send:&lt;br /&gt;
&lt;br /&gt;
 typedef enum message_request_t {&lt;br /&gt;
     GETS      = 0,&lt;br /&gt;
     GETM      = 1,&lt;br /&gt;
     PUTS      = 2,&lt;br /&gt;
     PUTM     = 3,&lt;br /&gt;
     DIR_FLUSH = 13&lt;br /&gt;
 } message_requests_enum_t;&lt;br /&gt;
&lt;br /&gt;
Types and functions here defined are extensively used by the protocol ROMs, at both Cache Controller- and Directory Controller-side.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Include&amp;diff=2261</id>
		<title>Include</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Include&amp;diff=2261"/>
				<updated>2019-07-01T11:15:48Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* User Defines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== NPU Defines == &lt;br /&gt;
Main NPU core defines are declared in the &amp;lt;code&amp;gt;npu_defines.sv&amp;lt;/code&amp;gt; file which stores global define at the core level. Parameters, such as the number of hardware lanes, number of register per register file, or memory address width, are defined in this file:&lt;br /&gt;
&lt;br /&gt;
 `define HW_LANE                 16&lt;br /&gt;
 `define ADDRESS_SIZE            32&lt;br /&gt;
 `define REGISTER_NUMBER         64&lt;br /&gt;
&lt;br /&gt;
along with information on special purpose registers: &lt;br /&gt;
&lt;br /&gt;
 `define PC_REG                  ( `REGISTER_NUMBER - 1 )&lt;br /&gt;
 `define RA_REG                  ( `REGISTER_NUMBER - 2 )&lt;br /&gt;
 `define SP_REG                  ( `REGISTER_NUMBER - 3 )&lt;br /&gt;
 `define FP_REG                  ( `REGISTER_NUMBER - 4 )&lt;br /&gt;
 `define MASK_REG                ( `REGISTER_NUMBER - 5 )&lt;br /&gt;
&lt;br /&gt;
and opcode definitions and instruction decoded-related data structure definitions:&lt;br /&gt;
&lt;br /&gt;
 NOT      = `OP_CODE_WIDTH'b000000,&lt;br /&gt;
 OR       = `OP_CODE_WIDTH'b000001,&lt;br /&gt;
 AND      = `OP_CODE_WIDTH'b000010,&lt;br /&gt;
 XOR      = `OP_CODE_WIDTH'b000011,&lt;br /&gt;
&lt;br /&gt;
== User Defines == &lt;br /&gt;
User defines are included into the file &amp;lt;code&amp;gt;npu_user_defines.sv&amp;lt;/code&amp;gt; which exposes to the final user all the configurable parameters of the architecture. Typically those parameters are bound to be a power of two.&lt;br /&gt;
&lt;br /&gt;
'''Core-side user defines''': all the following are bound to be a power of two&lt;br /&gt;
&lt;br /&gt;
* THREAD_NUMB: number of hardware thread instantiated, default 8.&lt;br /&gt;
* USER_ICACHE_SET: number of instruction cache sets, default 32.&lt;br /&gt;
* USER_ICACHE_WAY: number of instruction cache sets, default 4.&lt;br /&gt;
* USER_DCACHE_SET: number of data cache sets, default 32.&lt;br /&gt;
* USER_DCACHE_WAY: number of data cache sets, default 4.&lt;br /&gt;
* USER_L2CACHE_SET: number of L2 data cache sets, default 128.&lt;br /&gt;
* USER_L2CACHE_WAY: number of L2 data cache sets, default 8. &lt;br /&gt;
* NPU_SPM: when defined allocates a scratchpad memory in each NPU core.&lt;br /&gt;
* NPU_FPU: when defined allocates an FPU in each NPU core.&lt;br /&gt;
&lt;br /&gt;
'''System-side user defines''': &lt;br /&gt;
&lt;br /&gt;
* DIRECTORY_BARRIER: when defined the manycore system supports a distributed directory mechanism spread over all tiles. Otherwise, it allocates a single centralized directory. The single-core version always has a centralized synchronization master.&lt;br /&gt;
* CENTRAL_SYNCH_ID: Centralized directory ID, used only when DIRECTORY_BARRIER is undefined.&lt;br /&gt;
* NoC_X_WIDTH: Network-on-Chip mesh x dimension width, default value 2, must be a power of 2.&lt;br /&gt;
* NoC_Y_WIDTH: Network-on-Chip mesh x dimension width, default value 2, must be a 3power of 2.&lt;br /&gt;
* TILE_MEMORY_ID: Memory Tile ID.&lt;br /&gt;
* TILE_H2C_ID: Host interface Tile ID.&lt;br /&gt;
* TILE_NPU: number of tile with an NPU core.&lt;br /&gt;
* IO_MAP_BASE_ADDR: base address of the non-coherent memory space dedicated for IO devices, default value 0xFF00_0000.&lt;br /&gt;
* IO_MAP_SIZE: width of the non-coherent memory space dedicated for IO devices, default value 0x00FF_FF00.&lt;br /&gt;
&lt;br /&gt;
Furthermore, DISPLAY variables are defined, all commented by default. When a DISPLAY variable is active, it generates a file, under a folder named after the selected kernel. Each DISPLAY variable logs a defined kind of transaction, namely:&lt;br /&gt;
* DISPLAY_MEMORY: logs on file the memory state at the end of the kernel execution.&lt;br /&gt;
* DISPLAY_MEMORY_TRANS: logs on file all requests to the main memory.&lt;br /&gt;
* DISPLAY_MEMORY_CONTROLLER: displays on shell memory requests to the memory controller and its responses. &lt;br /&gt;
* DISPLAY_INT: logs every integer operation in the integer pipeline, and their results.&lt;br /&gt;
* DISPLAY_CORE: enables log from the core (file display_core.txt).&lt;br /&gt;
* DISPLAY_ISSUE: logs all scheduled instructions, and tracks the scheduled PC and the issued Thread, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_INT: logs all results from the integer module, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_WB: logs all results from the writeback module, when DISPLAY_CORE is defined.&lt;br /&gt;
* DISPLAY_LDST: enables logging into the load/store unit (file display_ldst.txt).&lt;br /&gt;
* DISPLAY_CACHE_CONTROLLER: logs memory transactions between Load/Store unit and the main memory.&lt;br /&gt;
* DISPLAY_SYNCH_CORE: logs synchronization requests within the core.&lt;br /&gt;
* DISPLAY_BARRIER_CORE: logs synchronization releases from the Synchronization master.&lt;br /&gt;
* DISPLAY_COHERENCE: logs all coherence transactions among CCs, DCs and MC.&lt;br /&gt;
* DISPLAY_THREAD_STATUS: displays all active threads status and trap reason.&lt;br /&gt;
&lt;br /&gt;
These variables selectively enable the logging of a specific feature. For each define, a log file is typically created in &amp;lt;code&amp;gt;npu/simulationlog/&amp;lt;name_of_the_kernel&amp;gt;/display_&amp;lt;name&amp;gt;&amp;lt;/code&amp;gt;. DISPLAY_SIMULATION_LOG variable has to be always defined in the simulation flow, this also displays architectural information on the tcl shell.&lt;br /&gt;
&lt;br /&gt;
== Scratchpad Memory Defines ==&lt;br /&gt;
&lt;br /&gt;
Defines related to the configuration of the scratchpad memory are stored into the &amp;lt;code&amp;gt;npu_spm_defines.sv&amp;lt;/code&amp;gt; file, which exposes to the final user all the configurable parameters related to the scratchpad memory at the core-level, along with other component-specific typedefs. The SPM has the following configurable parameters: &lt;br /&gt;
&lt;br /&gt;
* SM_PROCESSING_ELEMENTS: number of concurrent input requests, default value 16 (equal to the number of hardware lanes).&lt;br /&gt;
* SM_ENTRIES: number of entries per bank (similar to cache sets). &lt;br /&gt;
* SM_MEMORY_BANKS: number of banks, default value 16.&lt;br /&gt;
* SM_BYTE_PER_ENTRY: number of bytes per entry, default value 4.&lt;br /&gt;
&lt;br /&gt;
== Network Defines ==&lt;br /&gt;
&lt;br /&gt;
Network related defines are spread over two include files, namely &amp;lt;code&amp;gt;npu_message_service_defines.sv&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;npu_network_define.sv&amp;lt;/code&amp;gt;. The first defines shared typedefs used for the host interfacing mechanism over the Network-on-Chip. &lt;br /&gt;
The latter defines the length of flits and exposes two configuration parameters two the final user:&lt;br /&gt;
* VC_PER_PORT: number of Virtual Channel per router port (5 ports), default value 4, must be a power of 2.&lt;br /&gt;
* QUEUE_LEN_PER_VC: length of the FIFO for each Virtual Channel, default value 16, must be a power of 2.&lt;br /&gt;
&lt;br /&gt;
The remainder of the file defines structures that ease flit management:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     flit_type_t flit_type;&lt;br /&gt;
     vc_id_t vc_id;&lt;br /&gt;
     port_t next_hop_port;&lt;br /&gt;
     tile_address_t destination;&lt;br /&gt;
     tile_destination_t core_destination;&lt;br /&gt;
 } flit_header_t;&lt;br /&gt;
&lt;br /&gt;
 typedef logic [`PAYLOAD_W-1:0] flit_body_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     flit_header_t header;&lt;br /&gt;
     flit_body_t payload;&lt;br /&gt;
 } flit_t;&lt;br /&gt;
&lt;br /&gt;
== Synchronization Defines ==&lt;br /&gt;
&lt;br /&gt;
Synchronization related structures and types are declared in the &amp;lt;code&amp;gt;npu_sychronization_defines.sv&amp;lt;/code&amp;gt; file, which mainly defines a data structure that describes the synchronization message at the highest level of the network stack:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     barrier_t id_barrier;&lt;br /&gt;
     cnt_barrier_t cnt_setup;&lt;br /&gt;
     tile_id_t tile_id_source;&lt;br /&gt;
 } sync_account_message_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     barrier_t id_barrier;&lt;br /&gt;
     logic [$bits(cnt_barrier_t)+$bits(tile_id_t)-1:0] padding;&lt;br /&gt;
 } sync_release_message_t;&lt;br /&gt;
&lt;br /&gt;
Synchronization traffics are encapsulated in a sync_message_t and then in a service_message_t:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     sync_message_type_t sync_type;&lt;br /&gt;
&lt;br /&gt;
     union packed {&lt;br /&gt;
          sync_account_message_t account_mess;&lt;br /&gt;
          sync_release_message_t release_mess;&lt;br /&gt;
     } sync_mess;&lt;br /&gt;
 } sync_message_t;&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed{&lt;br /&gt;
     cnt_barrier_t cnt;&lt;br /&gt;
     tile_mask_t mask_slave;&lt;br /&gt;
 } barrier_data_t;&lt;br /&gt;
&lt;br /&gt;
== Coherence Defines == &lt;br /&gt;
&lt;br /&gt;
Coherence related defines are stored into &amp;lt;code&amp;gt;npu_coherence_defines.sv&amp;lt;/code&amp;gt; which declares all types and structures used by coherence actors (mainly '''Cache Controller''' and '''Directory Controller'''), such as TSHR entry type&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
     logic                                  valid;&lt;br /&gt;
     logic [`DIRECTORY_STATE_WIDTH - 1 : 0] state;&lt;br /&gt;
     l2_cache_address_t                     address;&lt;br /&gt;
     tile_mask_t                            sharers_list;&lt;br /&gt;
     tile_address_t                         owner;&lt;br /&gt;
 } tshr_entry_t;&lt;br /&gt;
&lt;br /&gt;
or the request a CC can accept:&lt;br /&gt;
&lt;br /&gt;
 typedef enum coherence_request_t {&lt;br /&gt;
     load                   = 0,&lt;br /&gt;
     store                  = 1,&lt;br /&gt;
     replacement            = 2,&lt;br /&gt;
     Fwd_GetS               = 3,&lt;br /&gt;
     Fwd_GetM               = 4,&lt;br /&gt;
     Inv                    = 5,&lt;br /&gt;
     Put_Ack                = 6,&lt;br /&gt;
     Data_from_Dir_ack_eqz  = 7,&lt;br /&gt;
     Data_from_Dir_ack_gtz  = 8,&lt;br /&gt;
     Data_from_Owner        = 9,&lt;br /&gt;
     Inv_Ack                = 10,&lt;br /&gt;
     Last_Inv_Ack           = 11,&lt;br /&gt;
     recall                 = 12,&lt;br /&gt;
     flush                  = 13,&lt;br /&gt;
     load_uncoherent        = 14,&lt;br /&gt;
     store_uncoherent       = 15,&lt;br /&gt;
     replacement_uncoherent = 16,&lt;br /&gt;
     flush_uncoherent       = 17,&lt;br /&gt;
     Fwd_Flush              = 18,&lt;br /&gt;
     dinv                   = 19,&lt;br /&gt;
     dinv_uncoherent        = 20&lt;br /&gt;
 } coherence_requests_enum_t;&lt;br /&gt;
&lt;br /&gt;
and, of course, kind of message a coherence actor can send:&lt;br /&gt;
&lt;br /&gt;
 typedef enum message_request_t {&lt;br /&gt;
     GETS      = 0,&lt;br /&gt;
     GETM      = 1,&lt;br /&gt;
     PUTS      = 2,&lt;br /&gt;
     PUTM     = 3,&lt;br /&gt;
     DIR_FLUSH = 13&lt;br /&gt;
 } message_requests_enum_t;&lt;br /&gt;
&lt;br /&gt;
Types and functions here defined are extensively used by the protocol ROMs, at both Cache Controller- and Directory Controller-side.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=SC_Logger&amp;diff=2260</id>
		<title>SC Logger</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=SC_Logger&amp;diff=2260"/>
				<updated>2019-07-01T11:13:34Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU single core system provides a logging system. The module npu_core_logger logs the last transactions to the main memory, logging all the memory request issued by the core and all the memory responses from the main memory. &lt;br /&gt;
&lt;br /&gt;
This module gets in input signal from the memory interface:&lt;br /&gt;
 // From the Memory&lt;br /&gt;
 input                                       mc_valid_i,&lt;br /&gt;
 input                  [ADDR_WIDTH - 1 : 0] mc_address_i,&lt;br /&gt;
 input                  [DATA_WIDTH - 1 : 0] mc_block_i,&lt;br /&gt;
&lt;br /&gt;
When mc_valid_i is hight, the logger stores in a dedicated SRAM (named u_mem_log) the current memory transaction information, namely memory address and fetched data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dually, the logger also gets in input all the core signals related to memory requests:&lt;br /&gt;
 // From the Core&lt;br /&gt;
 input                                       core_write_i,&lt;br /&gt;
 input                                       core_read_i,&lt;br /&gt;
 input                  [ADDR_WIDTH - 1 : 0] core_address_i,&lt;br /&gt;
 input                  [DATA_WIDTH - 1 : 0] core_block_i,&lt;br /&gt;
&lt;br /&gt;
In the same way the logger stores in a dedicated SRAM (named u_core_log) the current memory request issued by the core. Along with memory address and data, in the SRAM is also stored the type of operation to perform on memory (read or write).&lt;br /&gt;
&lt;br /&gt;
The logger provides a snooping interface connected on the item interface, allowing the host to fetch those information at run-time:&lt;br /&gt;
&lt;br /&gt;
 // Snoop Request&lt;br /&gt;
 input                                       snoop_valid_i,&lt;br /&gt;
 input  log_snoop_req_t                      snoop_request_i,&lt;br /&gt;
 input                  [ADDR_WIDTH - 1 : 0] snoop_addr_i,&lt;br /&gt;
&lt;br /&gt;
The snoop_request_i signal embeds the kind of request the host issued to the logger, those are defined in the &amp;lt;code&amp;gt;npu_system_defines.sv&amp;lt;/code&amp;gt; file and they are organized as an enum type as shown below:&lt;br /&gt;
&lt;br /&gt;
 typedef enum logic [`LOG_SNOOP_COMMANDS_WIDTH - 1 : 0]{&lt;br /&gt;
 	SNOOP_CORE = 0,&lt;br /&gt;
 	SNOOP_MEM  = 1,&lt;br /&gt;
 	GET_CORE_EVENTS = 2,&lt;br /&gt;
 	GET_MEM_EVENTS  = 3,&lt;br /&gt;
 	GET_EVENT_COUNTER  = 4&lt;br /&gt;
 } log_snoop_req_enum_t;&lt;br /&gt;
&lt;br /&gt;
In details:&lt;br /&gt;
&lt;br /&gt;
# SNOOP_CORE: query the core SRAM and fetch the i-th core transaction. The number of the transaction is specified in the snoop_addr_i signal.&lt;br /&gt;
# SNOOP_MEM: query the core SRAM and fetch the i-th memory transaction. The number of the transaction is specified in the snoop_addr_i signal.&lt;br /&gt;
# GET_CORE_EVENTS: get the number of core transactions recorded. &lt;br /&gt;
# GET_MEM_EVENTS: get the number of memory responses recorded.&lt;br /&gt;
# GET_EVENT_COUNTER: get the total number of transactions (core + memory transactions).&lt;br /&gt;
&lt;br /&gt;
Those counters help the user to have an idea of which is the last recorded transaction. The control logic of this unit overwrites old transactions if the SRAMs are full. The control logic uses SRAMs in a circular way. If the last memory location has been used, a new incoming transaction will be stored in the first position overwriting the old value.  &lt;br /&gt;
&lt;br /&gt;
A snooping request from the host is forwarded on the following output interface:&lt;br /&gt;
 // Log Output&lt;br /&gt;
 output logic                                cl_valid_o,&lt;br /&gt;
 output logic           [ADDR_WIDTH - 1 : 0] cl_req_addr_o,&lt;br /&gt;
 output logic           [DATA_WIDTH - 1 : 0] cl_req_data_o,&lt;br /&gt;
 output logic           [ADDR_WIDTH - 1 : 0] cl_req_id_o,&lt;br /&gt;
 output logic                                cl_req_is_write_o,&lt;br /&gt;
 output logic                                cl_req_is_read_o&lt;br /&gt;
&lt;br /&gt;
Those signals are actually unconnected and used for debugging purposes only in simulation.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=The_NaplesPU_Hardware_architecture&amp;diff=2259</id>
		<title>The NaplesPU Hardware architecture</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=The_NaplesPU_Hardware_architecture&amp;diff=2259"/>
				<updated>2019-07-01T11:11:57Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Configuring NaplesPU */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The '''NaplesPU''' many-core is a modular and deeply customizable system based on a regular mesh network-on-chip of configurable tiles, designed to be an extensible and&lt;br /&gt;
parametric platform from the ground up suitable for exploring advanced architectural solutions. Developed in the framework of the MANGO FETHPC&lt;br /&gt;
project, the main objective of NaplesPU is to enable resource-efficient HPC based on special-purpose customized hardware. This led to a modular design and&lt;br /&gt;
an instruction layout that exposes enough freedom to extend both the standard set of instructions and the baseline NPU hardware design, hereafter&lt;br /&gt;
discussed. This project aims to pose a fully customizable and easy to extend the many-core system, suitable for the exploration of both software and&lt;br /&gt;
hardware advanced solutions for tomorrow’s systems.&lt;br /&gt;
&lt;br /&gt;
The main objective of '''NaplesPU''' is to enable resource-efficient HPC based on special-purpose customized hardware. Our aim is to build an application-driven architecture to achieve the best hardware/software configuration for any data-parallel kernel. Specialized data-parallel accelerators have been known to provide higher efficiency than general-purpose processors for codes with significant amounts of regular data-level parallelism (DLP). However, every parallel kernel has its own ideal configuration.&lt;br /&gt;
&lt;br /&gt;
Each NPU tile has the same basic components, it provides a configurable GPU-like open-source soft-core meant to be used as a configurable FPGA overlay. This HPC-oriented accelerator merges the SIMT paradigm with vector processor model. Furthermore, each tile has a Cache Controller and a Directory Controller, those components handle data coherence between different cores in different tiles.&lt;br /&gt;
&lt;br /&gt;
[[File:npu_manycore.png|800px|NaplesPU manycore architecture]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Tile Overview ==&lt;br /&gt;
The figure above captures a simplified overview of the NaplesPU many-core. Each NPU tile has the same basic components, it provides a configurable GPU-like&lt;br /&gt;
accelerator meant to be used as a configurable FPGA overlay, an extendible coherence subsystem protocol-independent, and a mesh-based networking&lt;br /&gt;
system which routes hardware messages over the communication network. &lt;br /&gt;
&lt;br /&gt;
The system is based on a 2D mesh of heterogeneous tiles relying on a network-on-chip (NoC, described in [[Network|Network architecture]] section). The NoC routers are tightly coupled with network interface modules providing packet-based communication over four different virtual channels. A two-stage look-ahead router is used implementing a wormhole flit-based communication. The networking infrastructure allows both intra-tile and inter-tile communication. One virtual channel is dedicated to service message flows. In particular, the manycore system supports a distributed synchronization mechanism based on hardware barriers (section [[SC_Synch|Synchronization]]). Importantly, based on simple valid/ready interfaces, one can integrate any processing unit in each tile. &lt;br /&gt;
&lt;br /&gt;
The accelerator merges the SIMT paradigm with vector processor model. The GPU-like model exposes promising features for improved resource efficiency. &lt;br /&gt;
In fact, it provides hardware threads executing coupled with SIMD execution units, while reducing control overheads and hiding possibly long&lt;br /&gt;
latencies. This accelerator effectively exploits multi-threading, SIMD operations, and low-overhead control flow constructs, in addition to a range&lt;br /&gt;
of advanced architecture customization capabilities, in order to enable a high-level utilization of the underlying resources. &lt;br /&gt;
&lt;br /&gt;
To ensure scalability, the NaplesPU manycore implements a sparse directory approach and a distributed L2 cache, described in [[Coherence|Coherence architecture]] section. Each tile deploys a coherence maintenance infrastructure along with the accelerator. A cache controller handles the local processing unit’s memory requests, turning load and store misses into directory requests over the network. It also handles responses and forwarded requests coming from the network, updating the block state in compliance with the given coherence protocol, while the accelerator is totally unaware of it. &lt;br /&gt;
&lt;br /&gt;
The architectures of the cache and the directory controllers have been designed with flexibility in mind and are not bound to any specific coherence protocol. They are equipped with a configurable protocol ROM, which provides a simple means for coherence protocol extensions, as it precisely describes the actions to take for each request based on the current block state.&lt;br /&gt;
&lt;br /&gt;
== Hardware sections ==&lt;br /&gt;
This section presents the main hardware components and their interaction in the NaplesPU project.   &lt;br /&gt;
&lt;br /&gt;
===Common components===&lt;br /&gt;
Basic components used all over the design are hereafter described, along with custom types and the GPGPU core provided. Those components are also part of the single-core version of the project (described later on).&lt;br /&gt;
&lt;br /&gt;
* [[Include|Include]]&lt;br /&gt;
&lt;br /&gt;
* [[Basic_comps|Basic components]]&lt;br /&gt;
&lt;br /&gt;
* [[Core|NaplesPU GPGPU core architecture]]&lt;br /&gt;
&lt;br /&gt;
===Many Core System===&lt;br /&gt;
Many-core advanced features, such as coherence subsystem and synchronization mechanism, are hereafter described.&lt;br /&gt;
* [[MC_System|System]]&lt;br /&gt;
&lt;br /&gt;
* [[MC_Item|Item Interface]]&lt;br /&gt;
&lt;br /&gt;
* [[Coherence|Coherence architecture]] &lt;br /&gt;
&lt;br /&gt;
* [[Synchronization|Synchronization architecture]]&lt;br /&gt;
&lt;br /&gt;
* [[Network|Network architecture]]&lt;br /&gt;
&lt;br /&gt;
===Single Core Version===&lt;br /&gt;
The project provides a single-core version featuring a GPGPU core and a simplified caching system, hereafter described.&lt;br /&gt;
&lt;br /&gt;
* [[SC_System|System]]&lt;br /&gt;
&lt;br /&gt;
* [[SC_Item|Item Interface]]&lt;br /&gt;
&lt;br /&gt;
* [[SC_CC|Cache Controller]] &lt;br /&gt;
&lt;br /&gt;
* [[SC_Synch|Synchronization]]&lt;br /&gt;
&lt;br /&gt;
* [[SC_Logger|Logger]]&lt;br /&gt;
&lt;br /&gt;
* [[System deployment|System deployment on FPGA]]&lt;br /&gt;
&lt;br /&gt;
===Testing coherence subsystem===&lt;br /&gt;
The coherence subsystem comes along with a dedicated testbench, described below.&lt;br /&gt;
&lt;br /&gt;
* [[Coherence Injection|Coherence Injection]]&lt;br /&gt;
&lt;br /&gt;
== Configuring NaplesPU ==&lt;br /&gt;
&lt;br /&gt;
One of the most important aspects of NaplesPU is parametrization. Many features can be extended by changing the corresponding value in header files, such as cache dimensions. User design can set a high number of parameter for every need, such as: &lt;br /&gt;
* NoC topology and Tile number related features (in '''npu_user_defines.sv''' header file):&lt;br /&gt;
 NoC_X_WIDTH     - number of tile on the X dimension, must be a power of 2&lt;br /&gt;
 NoC_Y_WIDTH     - number of tile on the Y dimension, must be a power of 2&lt;br /&gt;
 TILE_MEMORY_ID  - tile ID of the memory controller, defines the position in the NoC&lt;br /&gt;
 TILE_H2C_ID     - tile ID of the host interface, defines the position in the NoC&lt;br /&gt;
 TILE_NPU        - number of NPU tile in the system&lt;br /&gt;
 TILE_HT         - number of heterogeneous tile in the system&lt;br /&gt;
* Core related parameters:&lt;br /&gt;
 THREAD_NUMB     - Threads per core, in '''npu_user_defines.sv''' header file.&lt;br /&gt;
 NPU_FPU         - Allocates the floating point unit in the NPU core.&lt;br /&gt;
 NPU_SPM         - Allocates a Scratchpad memory in each NPU core.&lt;br /&gt;
 HW_LANE         - Defines the width of the SIMD extension, in '''npu_defines.sv''' header file. &lt;br /&gt;
 REGISTER_NUMBER - Number of registers in both scalar and vectorial register files, in '''npu_defines.sv''' header file. Beware, changing the number of registers changes the position on special purpose register (such as PC or SP), the compiler has to be modified accordingly.&lt;br /&gt;
* Cache related parameters (in '''npu_user_defines.sv''' header file):&lt;br /&gt;
 USER_ICACHE_SET  - Number of sets in Instruction caches, must be a power of 2          &lt;br /&gt;
 USER_ICACHE_WAY  - Number of ways in Instruction caches, must be a power of 2                 &lt;br /&gt;
 USER_DCACHE_SET  - Number of sets in Data caches, must be a power of 2                 &lt;br /&gt;
 USER_DCACHE_WAY  - Number of ways in Data caches, must be a power of 2                 &lt;br /&gt;
 USER_L2CACHE_SET - Number of sets in L2 cache, must be a power of 2                 &lt;br /&gt;
 USER_L2CACHE_WAY - Number of ways in L2 cache, must be a power of 2       &lt;br /&gt;
* System related configurations (in '''npu_user_defines.sv''' header file):&lt;br /&gt;
 IO_MAP_BASE_ADDR  - Start of memory space allocated for IO operations (bypasses coherence). &lt;br /&gt;
 IO_MAP_SIZE       - Width of memory space for IO operations.        &lt;br /&gt;
 DIRECTORY_BARRIER - When defined the system supports a distributed directory over all tiles. Otherwise, it allocates a single synchronization master.&lt;br /&gt;
 CENTRAL_SYNCH_ID  - Single synchronization master ID, used only when DIRECTORY_BARRIER is undefined.    &lt;br /&gt;
&lt;br /&gt;
=== Changing number of thread ===&lt;br /&gt;
&lt;br /&gt;
As mentioned above, the number of threads is the same for each core and this parameter can easily be modified changing the '''THREAD_NUMB''' value in the '''npu_user_defines.sv''' header file. Each thread shares the L1 data and instruction cache. &lt;br /&gt;
&lt;br /&gt;
Changing the parameter value to the desired one is enough to obtain the thread number modification on the hardware side. Although the compiler has information on the number of threads, the linker uses those info to properly manage the stacks in the memory layout. The misc/lnkrscrpt.ld file in the toolchain repository has this information, in particular, the following row has to be updated coherently: &lt;br /&gt;
&lt;br /&gt;
 threads_per_core = 0x8;&lt;br /&gt;
&lt;br /&gt;
Such value is used in the crt0.s file in order to calculate stacks dimensions and locations.&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2258</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2258"/>
				<updated>2019-07-01T11:10:00Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Service Message Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user ought to assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; bit, which signals to the Network Interface that the message has been correctly received and handled. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following code snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
Field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it builds the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared in the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded using a bitmask, and each bit represents a different tile based on the position of the corresponding bit, e.g. bit in position 0 targets tile 0, and so on. The Network Interface connected will forward the message to each tile declared in such a mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward.&lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2257</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2257"/>
				<updated>2019-07-01T11:06:51Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Service Message Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user ought to assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; bit, which signals to the Network Interface that the message has been correctly received and handled. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following code snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
Field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it might build the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared through the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded in such a bitmask, and the Network Interface connected will forward the message to each tile declared in the mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward.&lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2256</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2256"/>
				<updated>2019-07-01T11:06:12Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Service Message Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user ought to assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; bit, which signals to the Network Interface that the message has been correctly received and handled. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following code snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
Field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such a cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it might build the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared through the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded in such a bitmask, and the Network Interface connected will forward the message to each tile declared in the mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward.&lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2255</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2255"/>
				<updated>2019-07-01T11:04:55Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Service Message Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user should assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; signal. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following code snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
Field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such a cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it might build the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared through the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded in such a bitmask, and the Network Interface connected will forward the message to each tile declared in the mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward.&lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2254</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2254"/>
				<updated>2019-07-01T11:04:44Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* Service Message Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user should assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; signal. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following code snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
The field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such a cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it might build the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared through the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded in such a bitmask, and the Network Interface connected will forward the message to each tile declared in the mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward.&lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2253</id>
		<title>Heterogeneous Tile</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Heterogeneous_Tile&amp;diff=2253"/>
				<updated>2019-07-01T11:03:58Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The NaplesPU project provides a heterogeneous tile integrated into the NoC, meant to be extended by the user. Such a tile provides a first example of how to integrate a custom module within the network-on-chip. The project comes along with a dedicated prototype in &amp;lt;code&amp;gt;src/mc/tile/tile_ht.sv&amp;lt;/code&amp;gt; meant to be extended for custom logic. &lt;br /&gt;
&lt;br /&gt;
[[File:Tile ht.png|470px]]&lt;br /&gt;
&lt;br /&gt;
The provided &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; module instantiates all modules typical of an NPU tile, except for the GPGPU core, and a modified version of the load/store unit wrapped into a simplified interface meant to be easily accessible for a custom component.&lt;br /&gt;
&lt;br /&gt;
The HT tile provides to users two main interfaces to interact with the system: the Memory Interface and the Synchronization Interface further explained below. Such a tile provides the following parameters for user-specific configurations:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot;npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot;npu_coherence_defines.sv&amp;quot;&lt;br /&gt;
 module tile_ht # (&lt;br /&gt;
    parameter TILE_ID        = 0,                 // Current tile ID&lt;br /&gt;
    parameter CORE_ID        = 0,                 // Current core ID, not used in this type of tile&lt;br /&gt;
    parameter TILE_MEMORY_ID = 9,                 // ID of the memory controller tile&lt;br /&gt;
    parameter THREAD_NUMB    = 8,                 // Supported thread number, each thread has a separate FIFO in the LSU and requests from different threads are elaborated concurrently - Must be a power of two&lt;br /&gt;
    parameter ADDRESS_WIDTH  = `ADDRESS_SIZE,     // Memory address width - has to be congruent with the system address width&lt;br /&gt;
    parameter DATA_WIDTH     = `CACHE_LINE_WIDTH, // Data bus width - has to be congruent with the system&lt;br /&gt;
    parameter L1_WAY_NUMB    = 4,                 // Number of way in the L1 data cache&lt;br /&gt;
    parameter L1_SET_NUMB    = 32,                // Number of L1 data sets&lt;br /&gt;
    parameter SYNCH_SUPP     = 1                  // Allocates barrier_core modules for synchronization support&lt;br /&gt;
 )&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
===Memory Interface===&lt;br /&gt;
The Memory Interface provides a transparent way to interact with the coherence system. The memory interface implements a simple valid/available handshake per&lt;br /&gt;
thread, a different thread might issue different memory transaction and those are concurrently handled by the coherence system. &lt;br /&gt;
&lt;br /&gt;
When a thread has a memory request, it first checks the availability bit related to its ID, if this is high the thread issues a memory transaction setting the valid bit and loading all the needed information on the Memory Interface. &lt;br /&gt;
&lt;br /&gt;
Supported memory operations are reported below along with their opcodes:&lt;br /&gt;
                                                                                  &lt;br /&gt;
 LOAD_8      = 'h0  - 'b000000    &lt;br /&gt;
 LOAD_16     = 'h1  - 'b000001&lt;br /&gt;
 LOAD_32     = 'h2  - 'b000010&lt;br /&gt;
 LOAD_V_8    = 'h7  - 'b000111&lt;br /&gt;
 LOAD_V_16   = 'h8  - 'b001000&lt;br /&gt;
 LOAD_V_32   = 'h9  - 'b001001&lt;br /&gt;
 STORE_8     = 'h20 - 'b100000&lt;br /&gt;
 STORE_16    = 'h21 - 'b100001&lt;br /&gt;
 STORE_32    = 'h22 - 'b100010&lt;br /&gt;
 STORE_V_8   = 'h24 - 'b100100&lt;br /&gt;
 STORE_V_16  = 'h25 - 'b100101&lt;br /&gt;
 STORE_V_32  = 'h26 - 'b100110&lt;br /&gt;
&lt;br /&gt;
A custom core to be integrate d in the NaplesPU system ought to implement the following interface in order to communicate with the memory system:&lt;br /&gt;
 /* Memory Interface */&lt;br /&gt;
 // To Heterogeneous LSU&lt;br /&gt;
 output logic                                 req_out_valid,     // Valid signal for issued memory requests&lt;br /&gt;
 output logic [31 : 0]                        req_out_id,        // ID of the issued request, mainly used for debugging&lt;br /&gt;
 output logic [THREAD_IDX_W - 1 : 0]          req_out_thread_id, // Thread ID of issued request. Requests running on different threads are dispatched to the CC conccurrently &lt;br /&gt;
 output logic [7 : 0]                         req_out_op,        // Operation performed&lt;br /&gt;
 output logic [ADDRESS_WIDTH - 1 : 0]         req_out_address,   // Issued request address&lt;br /&gt;
 output logic [DATA_WIDTH - 1    : 0]         req_out_data,      // Data output&lt;br /&gt;
 // From Heterogeneous LSU&lt;br /&gt;
 input  logic                                 resp_in_valid,      // Valid signal for the incoming responses&lt;br /&gt;
 input  logic [31 : 0]                        resp_in_id,         // ID of the incoming response, mainly used for debugging&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          resp_in_thread_id,  // Thread ID of the incoming response&lt;br /&gt;
 input  logic [7 : 0]                         resp_in_op,         // Operation code&lt;br /&gt;
 input  logic [DATA_WIDTH - 1 : 0]            resp_in_cache_line, // Incoming data&lt;br /&gt;
 input  logic [BYTES_PERLINE - 1 : 0]         resp_in_store_mask, // Bitmask of the position of the requesting bytes in the incoming data bus&lt;br /&gt;
 input  logic [ADDRESS_WIDTH - 1 : 0]         resp_in_address,    // Incoming response address&lt;br /&gt;
&lt;br /&gt;
The Heterogeneous Tile shares the same LSU and CC of a NPU tile, consequently the LSU forwards on the Memory Interface its backpressure signals, as follows:&lt;br /&gt;
 // From Heterogeneous accelerator - Backpressure signals&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_almost_full,           // Thread bitmask, if i-th bit is high, i-th thread cannot issue requests. &lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           lsu_het_no_load_store_pending, // Thread bitmask, if i-th bit is low, i-th thread has no pending operations. &lt;br /&gt;
In particular, ''lsu_het_almost_full'' i-th bit has to be low before issuing a memory request for i-th thread.&lt;br /&gt;
&lt;br /&gt;
The Memory Interface provides performance counters as part of its interface:&lt;br /&gt;
 // From Heterogeneous LSU - Performance counters&lt;br /&gt;
 input  logic                                 resp_in_miss,  // LSU miss on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_evict, // LSU eviction (replacement) on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_flush, // LSU flush on resp_in_address&lt;br /&gt;
 input  logic                                 resp_in_dinv,  // LSU data cache invalidatio on resp_in_address&lt;br /&gt;
Those signals state when the L1 Data cache incurs in a miss, eviction (or replacement), flush, and data cache invalidation.&lt;br /&gt;
&lt;br /&gt;
The LSU in the Heterogeneous Tile can be configured in two different modalities, namely Write-Through and Write-Back:&lt;br /&gt;
 output logic                                 lsu_het_ctrl_cache_wt,   // Enable Write-Through cache configuration.&lt;br /&gt;
When lsu_het_ctrl_cache_wt is high the LSU acts as a Write-Through cache, dually when low it implements a Write-Back mechanism.&lt;br /&gt;
&lt;br /&gt;
Finally, the Memory Interface provides error signals in case of address misaligned in the issued request:&lt;br /&gt;
 // Heterogeneous accelerator - Flush and Error signals&lt;br /&gt;
 input  logic                                 lsu_het_error_valid,     // Error coming from LSU&lt;br /&gt;
 input  register_t                            lsu_het_error_id,        // Error ID - Misaligned = 380&lt;br /&gt;
 input  logic [THREAD_IDX_W - 1 : 0]          lsu_het_error_thread_id, // Thread involved in the Error&lt;br /&gt;
&lt;br /&gt;
===Synchronization Interface===                                                  &lt;br /&gt;
The Synchronization Interface connects the user logic with the synchronization module core-side allocated within the tile (namely the &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; unit). &lt;br /&gt;
Such an interface allows user logic to synchronize on a thread grain, when parameter '''SYNCH_SUPP''' is high the tile implements the synchronization support allocating a &amp;lt;code&amp;gt;barrier_core&amp;lt;/code&amp;gt; module which handles the synchronization events core-side:&lt;br /&gt;
 // SYNCH_SUPP parameter allocates the following barrier_core, which&lt;br /&gt;
 // provides core-side synchronization support. &lt;br /&gt;
 generate&lt;br /&gt;
  if ( SYNCH_SUPP == 1) begin&lt;br /&gt;
    barrier_core # (&lt;br /&gt;
         .TILE_ID        ( TILE_ID     ),&lt;br /&gt;
         .THREAD_NUMB    ( THREAD_NUMB ),&lt;br /&gt;
         .MANYCORE       ( 1           ),&lt;br /&gt;
         .DIS_SYNCMASTER ( DISTR_SYNC  )&lt;br /&gt;
    )&lt;br /&gt;
    u_barrier_core (&lt;br /&gt;
        .clk                            ( clk                      ),&lt;br /&gt;
        .reset                          ( reset                    ),          &lt;br /&gt;
        // Synch Request - Core Interface&lt;br /&gt;
        .opf_valid                      ( breq_valid               ),&lt;br /&gt;
        .opf_inst_scheduled             ( bc_inst_scheduled        ),&lt;br /&gt;
        .opf_fetched_op0                ( breq_barrier_id          ),&lt;br /&gt;
        .opf_fetched_op1                ( breq_thread_numb         ),&lt;br /&gt;
        .bc_release_val                 ( bc_release_val           ),&lt;br /&gt;
         ...&lt;br /&gt;
    );&lt;br /&gt;
  end else begin&lt;br /&gt;
   assign bc_release_val                = {THREAD_NUMB{1'b1}};&lt;br /&gt;
   assign c2n_account_valid             = 1'b0;&lt;br /&gt;
   assign c2n_account_message           = sync_account_message_t'(0);&lt;br /&gt;
   assign c2n_account_destination_valid = tile_mask_t'(0);&lt;br /&gt;
   assign n2c_mes_service_consumed      = 1'b0;&lt;br /&gt;
  end&lt;br /&gt;
 endgenerate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The synchronization mechanism supports inter- and intra- tile barrier synchronization. When a thread hits a synchronization point, it issues a request to the distributed synchronization master through the Synchronization Interface.&lt;br /&gt;
Then, the thread is stalled (up to the user logic) till its release signal is high again.&lt;br /&gt;
&lt;br /&gt;
A custom core may implement the following interface if synchronization is requeried:&lt;br /&gt;
 /* Synchronization Interface */&lt;br /&gt;
 // To Barrier Core&lt;br /&gt;
 output logic                                 breq_valid,       // Hit barrier signal, sends a synchronization request&lt;br /&gt;
 output logic [31 : 0]                        breq_op_id,       // Synchronization operation ID, mainly used for debugging &lt;br /&gt;
 output logic [THREAD_NUMB - 1 : 0]           breq_thread_id,   // ID of the thread perfoming the synchronization operation&lt;br /&gt;
 output logic [31 : 0]                        breq_barrier_id,  // Barrier ID, has to be unique in case of concurrent barriers&lt;br /&gt;
 output logic [31 : 0]                        breq_thread_numb, // Total number - 1 of synchronizing threads on the current barrier ID&lt;br /&gt;
 // From Barrier Core&lt;br /&gt;
 input  logic [THREAD_NUMB - 1 : 0]           bc_release_val // Stalled threads bitmask waiting for release (the i-th bit low stalls the i-th thread)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Service Message Interface===                                                  &lt;br /&gt;
The Service Message Interface connects the user logic with the service network. The heterogeneous tile can send message to other nodes through this interface, this is often used for host-tile communication.&lt;br /&gt;
A custom core may implement the following interface if communication through messages is required:&lt;br /&gt;
&lt;br /&gt;
 /* Service Message Interface */&lt;br /&gt;
 // From Service Network&lt;br /&gt;
 input  logic                                 message_in_valid,         // Valid bit for incoming Service Message &lt;br /&gt;
 input  service_message_t                     message_in,               // Incoming message from Service Network&lt;br /&gt;
 output logic                                 n2c_mes_service_consumed, // Service Message consumed&lt;br /&gt;
 &lt;br /&gt;
 // To Service Network&lt;br /&gt;
 output logic                                 message_out_valid, // Valid bit for outcoming Service Message&lt;br /&gt;
 output service_message_t                     message_out,       // Outcoming Service Message data&lt;br /&gt;
 input  logic                                 network_available, // Service Network availability bit&lt;br /&gt;
 output tile_mask_t                           destination_valid  // One-Hot destinations bitmap&lt;br /&gt;
&lt;br /&gt;
The Service Message interface is a standard valid/available interface, when &amp;lt;code&amp;gt;message_in_valid&amp;lt;/code&amp;gt; is asserted an incoming message &amp;lt;code&amp;gt;message_in&amp;lt;/code&amp;gt; is valid, and the user should assert for a clock cycle the &amp;lt;code&amp;gt;n2c_mes_service_consumed&amp;lt;/code&amp;gt; signal. The incoming message is declared as a &amp;lt;code&amp;gt;service_message_t&amp;lt;/code&amp;gt; type, as shown in the following snippet:&lt;br /&gt;
&lt;br /&gt;
 typedef struct packed {&lt;br /&gt;
 	service_message_type_t message_type;&lt;br /&gt;
 	service_message_data_t data;&lt;br /&gt;
 } service_message_t;&lt;br /&gt;
&lt;br /&gt;
The field &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; stores the information received, while &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; states the nature of the incoming message. In such a cases this value might be &amp;lt;code&amp;gt;HOST&amp;lt;/code&amp;gt;, in case of message from the host, or &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; in case of a message from another heterogeneous tile.&lt;br /&gt;
&lt;br /&gt;
On the other hand, whenever the user logic has a message to send over the network, it might build the output message &amp;lt;code&amp;gt;message_out&amp;lt;/code&amp;gt;, storing the message body in the &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; field, while tying the &amp;lt;code&amp;gt;message_type&amp;lt;/code&amp;gt; filed to &amp;lt;code&amp;gt;HT_CORE&amp;lt;/code&amp;gt; value. Then, the destination tile (or tiles) has to be declared through the &amp;lt;code&amp;gt;destination_valid&amp;lt;/code&amp;gt; output bitmask, each tile in the network is decoded in such a bitmask, and the Network Interface connected will forward the message to each tile declared in the mask. When both message and destinations are ready, the user first checks the availability of the network reading the &amp;lt;code&amp;gt;network_available&amp;lt;/code&amp;gt; input bit, waiting for it if necessary. Then, the control logic might assert the &amp;lt;code&amp;gt;message_out_valid&amp;lt;/code&amp;gt; signal forwarding both message and destinatins to the Network Interface, which will take care of the delivery from this moment onward. &lt;br /&gt;
&lt;br /&gt;
== Heterogeneous Dummy provided ==&lt;br /&gt;
&lt;br /&gt;
This FSM first synchronizes with other ht in the NoC. Each dummy core in a ht tile requires a synchronization for LOCAL_BARRIER_NUMB threads (default = 4). &lt;br /&gt;
&lt;br /&gt;
 // Issue synchronization requests&lt;br /&gt;
 SEND_BARRIER    : begin&lt;br /&gt;
   breq_valid       &amp;lt;= 1'b1;&lt;br /&gt;
   breq_barrier_id  &amp;lt;= 42;&lt;br /&gt;
   barrier_served   &amp;lt;= 1'b1;&lt;br /&gt;
   if(rem_barriers == 1) &lt;br /&gt;
     next_state &amp;lt;= WAIT_SYNCH;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
The SEND_BARRIER state sends LOCAL_BARRIER_NUMB requests with barrier ID 42 through the Synchronization interface. It sets the total number of threads synchronizing on the barrier ID 42 equal to TOTAL_BARRIER_NUMB (=LOCAL_BARRIER_NUMB x `TILE_HT, number of heterogeneous tile in the system). When the last barrier is issued, SEND_BARRIER jumps to WAIT_SYNCH waiting for the ACK from the synchronization master.&lt;br /&gt;
&lt;br /&gt;
 // Synchronizes all dummy cores&lt;br /&gt;
 WAIT_SYNCH : begin&lt;br /&gt;
  if(&amp;amp;bc_release_val)&lt;br /&gt;
     next_state    &amp;lt;= IDLE;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
At this point, all threads in each ht tile are synchronized, and the FSM starts all pending memory transactions. &lt;br /&gt;
&lt;br /&gt;
The START_MEM_READ_TRANS performs LOCAL_WRITE_REQS read operations (default = 128), performing a LOAD_8 operation (op code = 0) each time. In the default configuration, 128 LOAD_8 operations on consecutive addresses are spread among all threads and issued to the LSU through the Memory interface. When read operations are over, the FSM starts writing operations in a similar way. &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple read operations&lt;br /&gt;
 START_MEM_READ_TRANS    : begin&lt;br /&gt;
   if ( rem_reads == 1 )&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_read] == 1'b0) begin&lt;br /&gt;
     read_served       &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_reads;&lt;br /&gt;
     req_out_op        &amp;lt;= 0; // LOAD_8&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_read;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The START_MEM_WRITE_TRANS performs LOCAL_WRITE_REQS (default = 128) write operations on consecutive addresses through the Memory interface. This time the operation performed is a STORE_8, and all ht tile are issuing the same store operation on the same addresses competing for the ownership in a transparent way. The coherence is totally handled by the LSU and CC, on the core side lsu_het_almost_full bitmap states the availability of the LSU for each thread (both writing and reading). &lt;br /&gt;
&lt;br /&gt;
 // Starting multiple write operations&lt;br /&gt;
 START_MEM_WRITE_TRANS    : begin&lt;br /&gt;
   if ( pending_writes )&lt;br /&gt;
     next_state &amp;lt;= IDLE;&lt;br /&gt;
   else&lt;br /&gt;
     next_state &amp;lt;= DONE;&lt;br /&gt;
   if(lsu_het_almost_full[thread_id_write] == 1'b0 ) begin&lt;br /&gt;
     write_served      &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_valid     &amp;lt;= 1'b1;&lt;br /&gt;
     req_out_id        &amp;lt;= rem_writes;&lt;br /&gt;
     req_out_thread_id &amp;lt;= thread_id_write;&lt;br /&gt;
     req_out_op        &amp;lt;= 'b100000; // STORE_8&lt;br /&gt;
     tmp_data_out[0]   &amp;lt;= 8'hee;&lt;br /&gt;
     incr_address      &amp;lt;= 1'b1;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In both states, a thread first checks the availability stored in a position equal to its ID (lsu_het_almost_full[thread_id]), then performs a memory transaction.&lt;br /&gt;
&lt;br /&gt;
== Adding custom logic ==&lt;br /&gt;
The provided heterogeneous dummy core might be replaced with a custom accelerator or user logic, that ought to be allocated into the &amp;lt;code&amp;gt;tile_ht&amp;lt;/code&amp;gt; replacing the following lines:&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 //  -- Tile HT - Dummy Core&lt;br /&gt;
 //  -----------------------------------------------------------------------&lt;br /&gt;
 het_core_example  #(&lt;br /&gt;
        .TILE_ID     ( TILE_ID     ),&lt;br /&gt;
        .THREAD_NUMB ( THREAD_NUMB )&lt;br /&gt;
 ) u_dummy_het_core 	(&lt;br /&gt;
        .clk                                                ( clk                           ),&lt;br /&gt;
        .reset                                              ( reset                         ),&lt;br /&gt;
         /* Memory Interface */&lt;br /&gt;
        .req_out_valid                                      ( req_in_valid                  ),&lt;br /&gt;
        .req_out_id                                         ( req_in_id                     ),&lt;br /&gt;
        .req_out_thread_id                                  ( req_in_thread_id              ),&lt;br /&gt;
        .req_out_op                                         ( req_in_op                     ),&lt;br /&gt;
        .req_out_address                                    ( req_in_address                ),&lt;br /&gt;
        .req_out_data                                       ( req_in_data                   ),&lt;br /&gt;
        ...&lt;br /&gt;
        /* Synchronization Interface */&lt;br /&gt;
        .breq_valid                                         ( breq_valid                    ),&lt;br /&gt;
        .breq_op_id                                         ( breq_op_id                    ),&lt;br /&gt;
        .breq_thread_id                                     ( breq_thread_id                ),&lt;br /&gt;
        .breq_barrier_id                                    ( breq_barrier_id               ),&lt;br /&gt;
        .breq_thread_numb                                   ( breq_thread_numb              ),&lt;br /&gt;
        ...&lt;br /&gt;
 );&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=Extending_NaplesPU&amp;diff=2252</id>
		<title>Extending NaplesPU</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=Extending_NaplesPU&amp;diff=2252"/>
				<updated>2019-07-01T10:36:18Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* SystemVerilog coding NaplesPU guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SystemVerilog coding NaplesPU guidelines ==&lt;br /&gt;
&lt;br /&gt;
This is a simple guideline for extending the NaplesPU architecture. &lt;br /&gt;
&lt;br /&gt;
1. the module's output signal names start ever with the mnemonic module name (e.g. writeback's signals -&amp;gt; wb_xxx).&lt;br /&gt;
&lt;br /&gt;
2. the testbench file name starts with tb_.&lt;br /&gt;
&lt;br /&gt;
3. add a folder for each different self-contained module and insert in the main folder &amp;quot;common&amp;quot; modules spread over all the project.&lt;br /&gt;
&lt;br /&gt;
4. use brackets for arithmetic operation in new defines.&lt;br /&gt;
&lt;br /&gt;
5. use structs or typedefs instead defines when subparts of a signal are often accessed.&lt;br /&gt;
&lt;br /&gt;
6. use divide et impera philosophy to improve re-usability and comprehensibility.&lt;br /&gt;
&lt;br /&gt;
7. use existing signal typedefs; if you introduce new structures and typedefs, allocate them in a specific header file for that component in the include folder (e.g. writeback unit -&amp;gt; writeback_defines.sv).&lt;br /&gt;
&lt;br /&gt;
== Adding custom instruction into NaplesPU core ==&lt;br /&gt;
&lt;br /&gt;
This section describes how to add new functional operation, extending the instruction set and adding a custom component into the NaplesPU pipeline.&lt;br /&gt;
&lt;br /&gt;
=== Defining a new instruction ===&lt;br /&gt;
&lt;br /&gt;
The first step is to add a new instruction in NaplesPU [[ISA]], starting with the instruction format. E.g., a new arithmetical operation ought to be part of R type instructions, while a new memory access instruction has to be part of the M type instructions. In the following example, we are introducing new arithmetical operation, called '''crp'''.  &lt;br /&gt;
&lt;br /&gt;
=== Extending compiler support ===&lt;br /&gt;
Extending the compiler support for a custom instruction involves two major steps:&lt;br /&gt;
* Adding a new intrinsic;&lt;br /&gt;
* Adding new instruction.&lt;br /&gt;
&lt;br /&gt;
==== Adding a new intrinsic ====&lt;br /&gt;
Adding a new intrinsic involves three different files in both back-end and front-end of the compiler.&lt;br /&gt;
&lt;br /&gt;
On the front-end side, Clang has to recognize this new intrinsic. This is accomplished by adding in the &amp;quot;compiler/tools/clang/include/clang/Basic/BuiltinsNaplesPU.def&amp;quot; file of the toolchain repo the following line:&lt;br /&gt;
&lt;br /&gt;
 //------ Cross Product ----------//&lt;br /&gt;
 BUILTIN ( __builtin_npu_crossprodv16i32 , &amp;quot; V16iV16iV16i &amp;quot;, &amp;quot;n&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Such a macro defines the signature of the intrinsic: &lt;br /&gt;
* ''__builtin_npu_crossprodv16i32'' - name&lt;br /&gt;
* ''V16iV16iV16i'' - input and output types&lt;br /&gt;
* ''n'' - optional attributes&lt;br /&gt;
&lt;br /&gt;
For further information, please refer to file &amp;quot;compiler/tools/clang/include/clang/Basic/Builtins.def&amp;quot; in the toolchain repo.&lt;br /&gt;
&lt;br /&gt;
Then, in &amp;quot;compiler/tools/clang/lib/CodeGen/CGBuiltin.cpp&amp;quot; we extend the ''EmitNPUBuiltinExpr'' method adding a new case in the switch construct as follow:&lt;br /&gt;
&lt;br /&gt;
 // Cross Product&lt;br /&gt;
 case NaplesPU :: BI__builtin_npu_crossprodv16i32 :&lt;br /&gt;
    F = CGM . getIntrinsic ( Intrinsic :: npu_crossprodv16i32 );&lt;br /&gt;
    break ;&lt;br /&gt;
&lt;br /&gt;
Keywork ''BI__builtin_npu_crossprodv16i32'' has to coherent with builtin added in the ''BuiltinsNaplesPU.def'' file, the signature name has to be preceded by ''BI''. &lt;br /&gt;
&lt;br /&gt;
Finally, we extend the ''compiler/include/llvm/IR/IntrinsicsNaplesPU.td'' file on the back-end side as follows:&lt;br /&gt;
 &lt;br /&gt;
 // Cross Product Intrinsic&lt;br /&gt;
 def int_npu_crossprodv16i32 : Intrinsic &amp;lt;[ llvm_v16i32_ty ], [ llvm_v16i32_ty , llvm_v16i32_ty ], [ IntrNoMem ], &amp;quot; llvm.npu.__builtin_npu_crossprodv16i32 &amp;quot;&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
This Table-Gen code adds the new intrinsic in Clang and generates the corresponding AST node.&lt;br /&gt;
&lt;br /&gt;
In questo modo si definisce un'istanza (int_npu_crossprodv16i32) della classe TableGen Intrinsic. Essa prevede di specificare rispettivamente i tipi di uscita e di ingresso (llvm_v16i32_ty), eventuali attributi (IntrNoMem) e la stringa di riconoscimento dell'IR (&amp;quot;llvm.npu.__builtin_npu_crossprodv16i32&amp;quot;) che deve contenere lo stesso nome della builtin definita in BuiltinsNaplesPU.def, da inserire dopo &amp;quot;llvm.npu.&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Adding new instruction ====&lt;br /&gt;
Adding a new instruction in the ISA at the back-end side of the compiler, we extend ''compiler/lib/Target/NaplesPU/NaplesPUInstrInfo.td'' file in the toolchain repo. Such an extensinon requipres Table-Gen classes defined in the ''compiler/lib/Target/NaplesPU/NaplesPUInstrFormats.td'' file. In particular, the classes used for the '''crp''' instruction is ''FR_TwoOp_Unmasked_32'', this defines the instruction as R type with two input operands (FR_TwoOp) both vectorials, with no mask (Unmasked_32):&lt;br /&gt;
&lt;br /&gt;
 // Cross Product Instruction&lt;br /&gt;
 def CROSSPROD_32 : FR_TwoOp_Unmasked_32 &amp;lt;&lt;br /&gt;
 ( outs VR512W : $dst ), // output&lt;br /&gt;
 ( ins VR512W :$src0 , VR512W : $src1 ), // input&lt;br /&gt;
 &amp;quot; crp $dst , $src0 , $src1 &amp;quot;, // corresponding assembly code&lt;br /&gt;
 [( set v16i32 :$dst , ( int_npu_crossprodv16i32 v16i32 :$src0 , v16i32 : $src1 ))], // matching pattern&lt;br /&gt;
 63, // ISA opcode (unique for instruction)&lt;br /&gt;
 Fmt_V , // destination register format&lt;br /&gt;
 Fmt_V , // src0 register format&lt;br /&gt;
 Fmt_V &amp;gt;; // src1 register format&lt;br /&gt;
&lt;br /&gt;
Attribute VR512W defines operation target register as vectors with 16 elements of 32-bit each. Dually, attribute Fmt_V sets accordingly in the FMT filed of the instruction bytecode.&lt;br /&gt;
For our custom module, we chose 63 as opcode further referred to as MY_OPCODE in the text.&lt;br /&gt;
&lt;br /&gt;
=== Extending NPU core pipeline ===&lt;br /&gt;
Extending NPU core pipeline with a new operator in the execution stage involves the following steps:&lt;br /&gt;
* defining new module and its interface;&lt;br /&gt;
* extending NPU Decode stage;&lt;br /&gt;
* extending NPU Writeback stage;&lt;br /&gt;
* adding the module in the NPU pipeline.&lt;br /&gt;
&lt;br /&gt;
Modules not fully pipelined have to extend ''Instruction_Buffer'' module as well.&lt;br /&gt;
&lt;br /&gt;
==== Custom unit interface ====&lt;br /&gt;
Follows an example interface:&lt;br /&gt;
&lt;br /&gt;
 `include &amp;quot; npu_user_defines.sv&amp;quot;&lt;br /&gt;
 `include &amp;quot; npu_defines.sv&amp;quot;&lt;br /&gt;
 module my_pipe (&lt;br /&gt;
    input clk ,&lt;br /&gt;
    input reset ,&lt;br /&gt;
    // To Instruction buffer&lt;br /&gt;
    output thread_mask_t my_stop ;&lt;br /&gt;
    // From Operand Fetch&lt;br /&gt;
    input opf_valid ,&lt;br /&gt;
    input instruction_decoded_t opf_inst_scheduled ,&lt;br /&gt;
    input vec_reg_size_t opf_fecthed_op0 ,&lt;br /&gt;
    input vec_reg_size_t opf_fecthed_op1 ,&lt;br /&gt;
    input hw_lane_mask_t opf_hw_lane_mask ,&lt;br /&gt;
    // To Writeback&lt;br /&gt;
    output logic my_valid ,&lt;br /&gt;
    output instruction_decoded_t my_inst_scheduled ,&lt;br /&gt;
    output vec_reg_size_t my_result ,&lt;br /&gt;
    output hw_lane_mask_t my_hw_lane_mask&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
If the new module cannot accept requests each clock cycles, it has to provide a stopping condition and to forward it to the Instruction_Buffer module in order to prevent further issues on the custom module. In the above example, this is done by ''my_stop'' signal, when the module cannot handle further requests it has to be high. Then add ''my_stop'' to the ''Instruction_Buffer'' module as follow:&lt;br /&gt;
&lt;br /&gt;
 assign ib_instructions_valid[thread_id] = ~fifo_empty &amp;amp; ~( l1d_full[thread_id] &amp;amp; ib_instructions[thread_id].pipe_sel == PIPE_MEM ) &amp;amp; enable &amp;amp; ~(my_stop &amp;amp; ib_instructions[thread_id].op_code == MY_OPCODE);&lt;br /&gt;
&lt;br /&gt;
The last step is not required if the custom module can handle a request per clock cycle.&lt;br /&gt;
&lt;br /&gt;
Input signals are generated by the ''Operand_Fetch'' module:&lt;br /&gt;
* opf_valid, incoming requests is valid.  &lt;br /&gt;
* instruction_decoded_t opf_inst_scheduled, current instruction decoded. The module has to check the op_code, if it is equal to the new opcode (MY_OPCODE) the issued operation has to be elaborated. An instruction can be either scalar or vectorial. These information are stored in the instruction_decoded_t fields and each register has a dedicated bit, namely ''is_source0_vectorial'', ''is_source1_vectorial'', and ''is_destination_vectorial'' bits.&lt;br /&gt;
* vec_reg_size_t opf_fecthed_op0, vector of registers in input.  &lt;br /&gt;
* vec_reg_size_t opf_fecthed_op1, vector of registers in input. &lt;br /&gt;
* hw_lane_mask_t opf_hw_lane_mask, hardware lane bitmask, the i-th bit states that the i-th element in the vector has to be elaborated.&lt;br /&gt;
&lt;br /&gt;
Output signals are forwarded to the ''Writeback'' module:&lt;br /&gt;
* my_valid, the output result is valid.&lt;br /&gt;
* instruction_decoded_t my_inst_scheduled, the module has to forward the issued instruction along with results.&lt;br /&gt;
* vec_reg_size_t my_result, output result organized in a vector lane.&lt;br /&gt;
* hw_lane_mask_t my_hw_lane_mask, the module has to forward the hardware bitmask used along with results.&lt;br /&gt;
&lt;br /&gt;
==== Extending Decode stage ====&lt;br /&gt;
First, we extend pipeline_disp_t type in include/npu_defines.sv with a new value which identifies our new module (es. PIPE_NEW). Then, in the same file, we add a new instruction to the right instruction type, we added a new R type instruction, hence in alu_op_t type add a new unique opcode (beware, as to be unique). In case of a new M type, we should have extended memory_op_t, and so on for the others (they are all in the same file).   &lt;br /&gt;
&lt;br /&gt;
Now, in the ''Decode'' stage, selects the right case in the switch construct based on the new instruction type, again in this example we refer to a new R type instruction, hence our code will be placed in the following case:&lt;br /&gt;
&lt;br /&gt;
 casez ( if_inst_scheduled.opcode )&lt;br /&gt;
   // RR&lt;br /&gt;
   8'b00_?????? : begin&lt;br /&gt;
   ...&lt;br /&gt;
&lt;br /&gt;
Be sure that in case of opcode = MY_OPCODE, the decode stage issues a new request for the custom modules by setting the pipe_sel value to PIPE_NEW, as follow:&lt;br /&gt;
&lt;br /&gt;
 if ( if_inst_scheduled.opcode.alu_opcode &amp;lt;= MOVE || ( if_inst_scheduled.opcode.alu_opcode &amp;gt;= SEXT8 &amp;amp; if_inst_scheduled.opcode.alu_opcode &amp;lt;= SEXT32 )&lt;br /&gt;
    || if_inst_scheduled.opcode.alu_opcode == MY_OPCODE ) begin&lt;br /&gt;
   if (if_inst_scheduled.opcode.alu_opcode == MY_OPCODE) begin&lt;br /&gt;
      instruction_decoded_next.pipe_sel = PIPE_NEW ;&lt;br /&gt;
      instruction_decoded_next.is_int = 1'b0;&lt;br /&gt;
      instruction_decoded_next.is_fp = 1'b0;&lt;br /&gt;
   end &lt;br /&gt;
&lt;br /&gt;
==== Extending Writeback stage ====&lt;br /&gt;
In the ''Writeback'' stage, we first add a new dedicated interface for the custom unit, as follow:&lt;br /&gt;
&lt;br /&gt;
 // From MY costum module&lt;br /&gt;
 input                                               my_valid,&lt;br /&gt;
 input  instruction_decoded_t                        my_inst_scheduled,&lt;br /&gt;
 input  hw_lane_t                                    my_result,&lt;br /&gt;
 input  hw_lane_mask_t                               my_mask_reg,&lt;br /&gt;
&lt;br /&gt;
Then, we add a new ''Writeback Request FIFO'' dedicated to fetch incoming results from our custom module. In this case, we update `NUM_EX_PIPE parameter in ''include/npu_defines.sv'' header (by adding one to the previous value), and adding a localparameter with a new ID for our custom operation:&lt;br /&gt;
 localparam  PIPE_FP_ID   = 0; // FP pipe FIFO index&lt;br /&gt;
 localparam  PIPE_INT_ID  = 1; // INT pipe FIFO index&lt;br /&gt;
 localparam  PIPE_SPM_ID  = 2; // SPM memory FIFO index&lt;br /&gt;
 localparam  PIPE_MEM_ID  = 3; // LDST unit FIFO index&lt;br /&gt;
 localparam  PIPE_NEW_ID  = 4; // NEW op FIFO index&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, we connect the dedicated FIFO to the interface inputs from the module:&lt;br /&gt;
&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].pc                        = my_inst_scheduled.pc;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].writeback_valid           = my_valid;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].thread_id                 = my_inst_scheduled.thread_id;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].writeback_result          = my_result;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].writeback_hw_lane_mask    = my_mask_reg;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].destination               = my_inst_scheduled.destination;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].is_destination_vectorial  = my_inst_scheduled.is_destination_vectorial;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].op_code                   = my_inst_scheduled.op_code;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].pipe_sel                  = my_inst_scheduled.pipe_sel;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].is_memory_access          = my_inst_scheduled.is_memory_access;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].has_destination           = my_inst_scheduled.has_destination;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].is_branch                 = my_inst_scheduled.is_branch;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].is_control                = my_inst_scheduled.is_control;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].is_movei                  = my_inst_scheduled.is_movei;&lt;br /&gt;
 assign input_wb_request[PIPE_NEW_ID].result_address            = 0;&lt;br /&gt;
&lt;br /&gt;
Finally, we extend the code which generates the writeback result forwarded to register files, adding a new case in the process that builds the ''output_wb_request.writeback'' signal:&lt;br /&gt;
 // Output data composer. The wb_result_data are directly forwarded to the register files&lt;br /&gt;
 always_comb begin : WB_OUTPUT_DATA_SELECTION&lt;br /&gt;
  case ( output_wb_request[selected_pipe].pipe_sel )&lt;br /&gt;
   PIPE_MEM : wb_next.wb_result_data = result_data_mem;&lt;br /&gt;
   PIPE_SPM : wb_next.wb_result_data = result_data_spm;&lt;br /&gt;
   PIPE_INT,&lt;br /&gt;
   '''PIPE_NEW''',&lt;br /&gt;
   PIPE_FP  : wb_next.wb_result_data = output_wb_request[selected_pipe].writeback_result;&lt;br /&gt;
   default : wb_next.wb_result_data  = 0;&lt;br /&gt;
   endcase&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==== Adding the module in the NPU pipeline ====&lt;br /&gt;
First, we declare signals needed by our module: &lt;br /&gt;
&lt;br /&gt;
 // NEW Pipe Stage - Signals&lt;br /&gt;
 logic                                        my_valid;&lt;br /&gt;
 instruction_decoded_t                        my_inst_scheduled;&lt;br /&gt;
 hw_lane_t                                    my_result;&lt;br /&gt;
 hw_lane_mask_t                               my_hw_lane_mask;&lt;br /&gt;
&lt;br /&gt;
Then, we drop our module instatiation in the NPU pipeline located into ''core/npu_core.sv'' file as follow:&lt;br /&gt;
&lt;br /&gt;
 my_pipe u_my_pipe (&lt;br /&gt;
  .clk                ( clk                ),&lt;br /&gt;
  .reset              ( reset              ),&lt;br /&gt;
  .enable             ( nfreeze            ),&lt;br /&gt;
  //From Operand Fetch&lt;br /&gt;
  .opf_valid          ( opf_valid          ),&lt;br /&gt;
  .opf_inst_scheduled ( opf_inst_scheduled ),&lt;br /&gt;
  .opf_fetched_op0    ( opf_fetched_op0    ),&lt;br /&gt;
  .opf_fetched_op1    ( opf_fetched_op1    ),&lt;br /&gt;
  .opf_hw_lane_mask   ( opf_hw_lane_mask   ),&lt;br /&gt;
  //To Writeback&lt;br /&gt;
  .my_valid           ( my_valid           ),&lt;br /&gt;
  .my_inst_scheduled  ( my_inst_scheduled  ),&lt;br /&gt;
  .my_result          ( my_result          ),&lt;br /&gt;
  .my_hw_lane_mask    ( my_hw_lane_mask    )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
Finally, we connect our module to the ''Writeback'' stage:&lt;br /&gt;
 writeback #(&lt;br /&gt;
  .TILE_ID( TILE_ID )&lt;br /&gt;
 )&lt;br /&gt;
 u_writeback (&lt;br /&gt;
   .clk                 ( clk                ),&lt;br /&gt;
   .reset               ( reset              ),&lt;br /&gt;
   .enable              ( 1'b1               ),	&lt;br /&gt;
  ...&lt;br /&gt;
  //From NEW Pipe&lt;br /&gt;
  .my_valid           ( my_valid           ),&lt;br /&gt;
  .my_inst_scheduled  ( my_inst_scheduled  ),&lt;br /&gt;
  .my_result          ( my_result          ),&lt;br /&gt;
  .my_hw_lane_mask    ( my_hw_lane_mask    )&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2249</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2249"/>
				<updated>2019-06-28T13:38:58Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) not used. Reserved for 64-bit extension.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for both memory operations to the main memory and the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix. E.g &amp;lt;code&amp;gt;load32_s8&amp;lt;/code&amp;gt; targets the main memory, while &amp;lt;code&amp;gt;load32_s8_scratchpad&amp;lt;/code&amp;gt; refers to a load operation for the on-chip scratchpad.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load32_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load memory byte [7:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_s16&lt;br /&gt;
| 1&lt;br /&gt;
| oad memory half word [15:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32&lt;br /&gt;
| 2&lt;br /&gt;
| load memory word into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load memory byte [7:0] with zero extension into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load memory half word [15:0] with zero extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i8&lt;br /&gt;
| 7&lt;br /&gt;
| load 16 byte [127:0] with sign extension into a 512 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i16&lt;br /&gt;
| 8&lt;br /&gt;
| load 16 half word [255:0] with sign extension &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i32&lt;br /&gt;
| 9&lt;br /&gt;
| load 16 words&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u8&lt;br /&gt;
| 11&lt;br /&gt;
| load 16 byte [127:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u16&lt;br /&gt;
| 12&lt;br /&gt;
| load 16 half word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v8u32&lt;br /&gt;
| 13&lt;br /&gt;
| load 8 word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| loadg32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses (only for scratchpad)&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store32_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i8&lt;br /&gt;
| 36&lt;br /&gt;
| store 16 bytes from a vectorial register (data fecthing from register schema [487:480,...,39:32,7:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i16&lt;br /&gt;
| 37&lt;br /&gt;
| store 16 half words (data fetching from register schema [495:480,...,47:32,15:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i32&lt;br /&gt;
| 38&lt;br /&gt;
| store 16 words from a vectorial register into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| stores32&lt;br /&gt;
| 42&lt;br /&gt;
| scatter store - store 16 words into 16 different addresses (only for scratchpad)&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2248</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2248"/>
				<updated>2019-06-28T13:38:02Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) not used. Reserved for 64-bit extension.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix. E.g &amp;lt;code&amp;gt;load_32_s8&amp;lt;/code&amp;gt; targets the main memory, while &amp;lt;code&amp;gt;load_32_s8_scratchpad&amp;lt;/code&amp;gt; refers to a load operation for the on-chip scratchpad.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load32_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load memory byte [7:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_s16&lt;br /&gt;
| 1&lt;br /&gt;
| oad memory half word [15:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32&lt;br /&gt;
| 2&lt;br /&gt;
| load memory word into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load memory byte [7:0] with zero extension into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load memory half word [15:0] with zero extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i8&lt;br /&gt;
| 7&lt;br /&gt;
| load 16 byte [127:0] with sign extension into a 512 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i16&lt;br /&gt;
| 8&lt;br /&gt;
| load 16 half word [255:0] with sign extension &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i32&lt;br /&gt;
| 9&lt;br /&gt;
| load 16 words&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u8&lt;br /&gt;
| 11&lt;br /&gt;
| load 16 byte [127:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u16&lt;br /&gt;
| 12&lt;br /&gt;
| load 16 half word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v8u32&lt;br /&gt;
| 13&lt;br /&gt;
| load 8 word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| loadg32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses (only for scratchpad)&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store32_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i8&lt;br /&gt;
| 36&lt;br /&gt;
| store 16 bytes from a vectorial register (data fecthing from register schema [487:480,...,39:32,7:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i16&lt;br /&gt;
| 37&lt;br /&gt;
| store 16 half words (data fetching from register schema [495:480,...,47:32,15:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i32&lt;br /&gt;
| 38&lt;br /&gt;
| store 16 words from a vectorial register into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| stores32&lt;br /&gt;
| 42&lt;br /&gt;
| scatter store - store 16 words into 16 different addresses (only for scratchpad)&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2247</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2247"/>
				<updated>2019-06-28T13:35:53Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) not used. Reserved for 64-bit extension.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load32_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load memory byte [7:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_s16&lt;br /&gt;
| 1&lt;br /&gt;
| oad memory half word [15:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32&lt;br /&gt;
| 2&lt;br /&gt;
| load memory word into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load memory byte [7:0] with zero extension into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load memory half word [15:0] with zero extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i8&lt;br /&gt;
| 7&lt;br /&gt;
| load 16 byte [127:0] with sign extension into a 512 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i16&lt;br /&gt;
| 8&lt;br /&gt;
| load 16 half word [255:0] with sign extension &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i32&lt;br /&gt;
| 9&lt;br /&gt;
| load 16 words&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u8&lt;br /&gt;
| 11&lt;br /&gt;
| load 16 byte [127:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u16&lt;br /&gt;
| 12&lt;br /&gt;
| load 16 half word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v8u32&lt;br /&gt;
| 13&lt;br /&gt;
| load 8 word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| loadg32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses (only for scratchpad)&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store32_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i8&lt;br /&gt;
| 36&lt;br /&gt;
| store 16 bytes from a vectorial register (data fecthing from register schema [487:480,...,39:32,7:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i16&lt;br /&gt;
| 37&lt;br /&gt;
| store 16 half words (data fetching from register schema [495:480,...,47:32,15:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i32&lt;br /&gt;
| 38&lt;br /&gt;
| store 16 words from a vectorial register into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| stores32&lt;br /&gt;
| 42&lt;br /&gt;
| scatter store - store 16 words into 16 different addresses (only for scratchpad)&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2246</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2246"/>
				<updated>2019-06-28T13:34:39Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) not used. Reserved for 64-bit extension.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load32_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load memory byte [7:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_s16&lt;br /&gt;
| 1&lt;br /&gt;
| oad memory half word [15:0] with sign extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32&lt;br /&gt;
| 2&lt;br /&gt;
| load memory word into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load memory byte [7:0] with zero extension into a 32 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load memory half word [15:0] with zero extension into a 32 bit register &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i8&lt;br /&gt;
| 7&lt;br /&gt;
| load 16 byte [127:0] with sign extension into a 512 bit register&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i16&lt;br /&gt;
| 8&lt;br /&gt;
| load 16 half word [255:0] with sign extension &lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16i32&lt;br /&gt;
| 9&lt;br /&gt;
| load 16 words&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u8&lt;br /&gt;
| 11&lt;br /&gt;
| load 16 byte [127:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v16u16&lt;br /&gt;
| 12&lt;br /&gt;
| load 16 half word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load_v8u32&lt;br /&gt;
| 13&lt;br /&gt;
| load 8 word [255:0] with no sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| loadg32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses (only for scratchpad)&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store32_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word into the effective address&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i8&lt;br /&gt;
| 36&lt;br /&gt;
| store 16 bytes from a vectorial register (data fecthing from register schema [487:480,...,39:32,7:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i16&lt;br /&gt;
| 37&lt;br /&gt;
| store 16 half words (data fetching from register schema [495:480,...,47:32,15:0]) into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store_v16i32&lt;br /&gt;
| 38&lt;br /&gt;
| store 16 words from a vectorial register into effective address location&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| stores32&lt;br /&gt;
| 42&lt;br /&gt;
| scatter store - store 16 words into 16 different addresses (only for scratchpad)&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2245</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2245"/>
				<updated>2019-06-28T13:21:33Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=File:M_format_new.png&amp;diff=2244</id>
		<title>File:M format new.png</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=File:M_format_new.png&amp;diff=2244"/>
				<updated>2019-06-28T13:21:20Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2243</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2243"/>
				<updated>2019-06-28T13:21:10Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2242</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2242"/>
				<updated>2019-06-28T13:20:58Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format_new.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2241</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2241"/>
				<updated>2019-06-28T13:19:27Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=File:M_format.png&amp;diff=2240</id>
		<title>File:M format.png</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=File:M_format.png&amp;diff=2240"/>
				<updated>2019-06-28T13:19:17Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2239</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2239"/>
				<updated>2019-06-28T13:19:03Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* M type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
This is the format of the M-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:M_format.png]]&lt;br /&gt;
&lt;br /&gt;
The fields of the M-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For M-type instructions, it is only 6 bits.&lt;br /&gt;
* rd/rs (B23-18)  is the destination or source register&lt;br /&gt;
* rbase (B17-12) is the base address register.&lt;br /&gt;
* offset (B11-3) is the offset address.&lt;br /&gt;
* bit l (B2) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bit s (B1) is used to specify if a certain load/store memory operation goes to the scratchpad memory or not. For instance, in case of a load/store from/to the scratchpad memory, B1=1, otherwise B1=0.&lt;br /&gt;
&lt;br /&gt;
The typical M type instructions are load and store instructions. In both cases, the source/destination address is calculated as base register address + immediate offset, i.e. rbase + offset. In case of load, rd = [rbase+offset]. Similarly, in case of store, [rbase + offset] = rs. All M type instructions can be used for memory operations towards the main memory or the scratchpad memory. Instructions that operate with the scratchpad memory have the &amp;lt;code&amp;gt;_scratchpad&amp;lt;/code&amp;gt; suffix.&lt;br /&gt;
&lt;br /&gt;
The M-type instructions can be classified in scalar and vector instructions. The scalar M-type instructions are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2238</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2238"/>
				<updated>2019-06-28T13:16:53Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* J type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=File:J_format.png&amp;diff=2237</id>
		<title>File:J format.png</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=File:J_format.png&amp;diff=2237"/>
				<updated>2019-06-28T13:16:44Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2236</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2236"/>
				<updated>2019-06-28T13:16:35Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* J type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
This is the format of the J-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:J_format.png]]&lt;br /&gt;
&lt;br /&gt;
The fields of the J-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For J-type instructions, it is only 3 bits.&lt;br /&gt;
* rcond/rd (B23-18) is the condition/destination register.&lt;br /&gt;
* offset (B17-0) is the offset address.&lt;br /&gt;
&lt;br /&gt;
The J-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2235</id>
		<title>ISA</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=ISA&amp;diff=2235"/>
				<updated>2019-06-28T13:15:34Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: /* C type instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Register File =&lt;br /&gt;
The NPU register file is  composed by a '''scalar''' register file and a '''vector''' register file; each one containing 64 registers. &lt;br /&gt;
&lt;br /&gt;
The scalar register file has 64 registers. The first 58 are general purpose registers, while the remaining 8 are special purpose registers. Each scalar register can store up to 32 bits of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:ScalarRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The vector register file has 64 general purpose registers&lt;br /&gt;
Each vector register can store up to 512 bits of data, each vector can store 16 x 32 bits.&lt;br /&gt;
&lt;br /&gt;
[[File:VectorRegFile_new.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, there is a Control Register that is composed of several sub-registers. Some information are shared among all threads, others are thread specific and those registers marked 'thread' have a separate instance per thread.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Read/Write&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Shared/Thread&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Description&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | ID&lt;br /&gt;
|-&lt;br /&gt;
| TILE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Tile ID&lt;br /&gt;
| 0&lt;br /&gt;
|-&lt;br /&gt;
| CORE_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Core ID&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_ID &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| ThreadID&lt;br /&gt;
| 2&lt;br /&gt;
|-&lt;br /&gt;
| GLOBAL_ID&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Global ID,  previous IDs merged as follow: TILE_ID, CORE_ID,  THREAD_ID&lt;br /&gt;
| 3&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_LOW &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Low part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 4&lt;br /&gt;
|-&lt;br /&gt;
| GCOUNTER_HIGH &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| High part of the Global counter register which counts processor cycles since reset&lt;br /&gt;
| 5&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_EN &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Thread enabled mask, 1 bit per thread&lt;br /&gt;
| 6&lt;br /&gt;
|-&lt;br /&gt;
| MISS_DATA &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Data cache misses&lt;br /&gt;
| 7&lt;br /&gt;
|-&lt;br /&gt;
| MISS_INSTR &lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| Count of L1 Instruction cache misses&lt;br /&gt;
| 8&lt;br /&gt;
|-&lt;br /&gt;
| PC &lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Current PC&lt;br /&gt;
| 9&lt;br /&gt;
|-&lt;br /&gt;
| TRAP_REASON&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| Trap Cause (see below)&lt;br /&gt;
| 10&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_STATUS&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Thread&lt;br /&gt;
| Thread Status2 (see below)&lt;br /&gt;
| 11&lt;br /&gt;
|-&lt;br /&gt;
| ARGC&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of strings pointed to by argv&lt;br /&gt;
| 12&lt;br /&gt;
|-&lt;br /&gt;
| ARGV&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| The address of command line arguments passed to main()&lt;br /&gt;
| 13&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_NUMB&lt;br /&gt;
| Read&lt;br /&gt;
| Shared&lt;br /&gt;
| The number of total hardware threads&lt;br /&gt;
| 14&lt;br /&gt;
|-&lt;br /&gt;
| THREAD_MISS_CC&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread clock cycles while the thread is idle due memory operations. &lt;br /&gt;
| 15&lt;br /&gt;
|-&lt;br /&gt;
| KERNEL_WORK&lt;br /&gt;
| Read&lt;br /&gt;
| Thread&lt;br /&gt;
| The per-thread kernel clock cycles. &lt;br /&gt;
| 16&lt;br /&gt;
|-&lt;br /&gt;
| CPU_CTRL_REG&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| CPU mode register. At the moment only write policy used by the cache controller is implemented. 0 for write-back, 1 for write-through&lt;br /&gt;
| 17&lt;br /&gt;
|-&lt;br /&gt;
| UNCOHERENCE_MAP&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Address the non-coherent table in the control register. It stores information about the non-coherent memory regions. User can define non-coherent regions addressing this special purpose register.&lt;br /&gt;
| 19&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG_BASE_ADDR&lt;br /&gt;
| Read/Write&lt;br /&gt;
| Shared&lt;br /&gt;
| Debug registers base address. The NPU is equipped with 16 debug registers. DEBUG_BASE_ADDR fetches the value of the first debug register, DEBUG_BASE_ADDR+1 the second and so on.&lt;br /&gt;
| 20&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Trap Cause: in the current state only traps due to misaligned memory accesses can raise:&lt;br /&gt;
# SPM_ADDR_MISALIGN: Misaligned memory access in the SPM unit.&lt;br /&gt;
# LDST_ADDR_MISALIGN: Misaligned memory access in the LDST unit.&lt;br /&gt;
Thread Status: each thread can be in one of the following states:&lt;br /&gt;
# THREAD_IDLE (Value = 0): each thread starts in this state after reset.&lt;br /&gt;
# RUNNING (Value = 1): the thread is running a kernel.&lt;br /&gt;
# END_MODE (Value = 2): the thread switches in this mode when the issued kernel is completed.&lt;br /&gt;
# TRAPPED (Value = 3): the thread is in trap mode. At the current state, when a trap occurs, the thread jumps into an infinite loop.&lt;br /&gt;
# WAITING_BARRIER (Value = 4): the thread is waiting for a synchronization event.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Types ===&lt;br /&gt;
The following table sums up the data types that are possible to use in NPU core. The '''Type''' column has the C/C++ type names, the '''LLVM type''' column presents the type names used in LLVM and the '''Register''' column shows the register type in which a value of a specific type is stored.&lt;br /&gt;
&lt;br /&gt;
The highlighted types are those the architecture natively supports, given the register files width. The others are obtained through extension so that they can be seen as the supported ones. Their advantage resides in more efficient use of the system memory. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | LLVM Type&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Register&lt;br /&gt;
! style=&amp;quot;font-weight: bold;&amp;quot; | Notes&lt;br /&gt;
|-&lt;br /&gt;
| bool&lt;br /&gt;
| i1&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| i8&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| i16&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| It is expanded to 32 bits&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | int&lt;br /&gt;
| i32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | float&lt;br /&gt;
| f32&lt;br /&gt;
| scalar (32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec16i8, vec16u8&lt;br /&gt;
| v16i8&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec16i16, vec16u16&lt;br /&gt;
| v16i16&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16i32, vec16u32&lt;br /&gt;
| v16i32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;font-weight: bold;&amp;quot; | vec16f32&lt;br /&gt;
| v16f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| vec8i8, vec8u8&lt;br /&gt;
| v8i8&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i16, vec8u16&lt;br /&gt;
| v8i16&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8i32, vec8u32&lt;br /&gt;
| v8i32&lt;br /&gt;
| vector (8 x 32 bits)&lt;br /&gt;
| It is expanded to 32 bits vector&lt;br /&gt;
|-&lt;br /&gt;
| vec8f32&lt;br /&gt;
| v8f32&lt;br /&gt;
| vector (16 x 32 bits)&lt;br /&gt;
| It is considered as a 16 elements vector&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Instructions Format =&lt;br /&gt;
The NaplesPU instructions have a fixed length of '''32 bits'''. They are grouped in '''six''' types:&lt;br /&gt;
&lt;br /&gt;
*The '''[[#R type instructions|R type]]''' includes the logical and arithmetic operations and memory operations.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#I type instructions|I type]]''' includes the logical and arithmetic operations between a register operand and an immediate operand.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#MOVEI type instructions|MOVEI type]]''' includes the load operations of an immediate operand in a register.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#C type instructions|C type]]''' used for control operations and for synchronization instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#J type instructions|J type]]''' includes jump instructions.&lt;br /&gt;
&lt;br /&gt;
*The '''[[#M type instructions|M type]]''' includes the instructions used to access memory.&lt;br /&gt;
&lt;br /&gt;
[[File:ISA.jpg|800px|ISA]]&lt;br /&gt;
&lt;br /&gt;
== R type instructions ==&lt;br /&gt;
This is the format of the R-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:RR_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
* RR (Register to Register) has a destination register and two source registers.&lt;br /&gt;
* RI (Register Immediate) has a destination register and one source registers and an immediate encoded in the instruction word.&lt;br /&gt;
&lt;br /&gt;
The fields of the R-type instruction are:&lt;br /&gt;
* opcode (B29-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For R-type instructions, it is only 6 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs0 (B17-12) is the first source register.&lt;br /&gt;
* rs1 (B11-6) is the second source register.&lt;br /&gt;
* bit l (B4) is used in case of &amp;quot;long&amp;quot; operations, i.e. operations that require long integers or double precision numbers. If the operation requires 64-bit registers l=1, otherwise l=0.&lt;br /&gt;
* bits fmt (B3-1) are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B3 refers to register d, B2 refers to register rs0 and B1 refers to register rs1. For instance, if the destination register should contain a vector, B3=1, otherwise B3=0.&lt;br /&gt;
&lt;br /&gt;
The R-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! or&lt;br /&gt;
! 1&lt;br /&gt;
! or&lt;br /&gt;
! Rd = Ra | Rb&lt;br /&gt;
|-&lt;br /&gt;
| and&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Rb&lt;br /&gt;
|-&lt;br /&gt;
| xor&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Rb&lt;br /&gt;
|-&lt;br /&gt;
| add&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| sub&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| mullo&lt;br /&gt;
| 6&lt;br /&gt;
| low result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| mulhu&lt;br /&gt;
| 8&lt;br /&gt;
| unsigned high result of the multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| ashr&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra '&amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shr&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| shl&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| clz&lt;br /&gt;
| 12&lt;br /&gt;
| count leading zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| ctz&lt;br /&gt;
| 13&lt;br /&gt;
| count trailing zeros&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| shuffle&lt;br /&gt;
| 24&lt;br /&gt;
| vector shuffle&lt;br /&gt;
| Rd[i] = Ra[Rb[i]]&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Rb]&lt;br /&gt;
|-&lt;br /&gt;
| move&lt;br /&gt;
| 32&lt;br /&gt;
| move register&lt;br /&gt;
| Rd = Ra&lt;br /&gt;
|-&lt;br /&gt;
| fadd&lt;br /&gt;
| 33&lt;br /&gt;
| floating point add&lt;br /&gt;
| Rd = Ra + Rb&lt;br /&gt;
|-&lt;br /&gt;
| fsub&lt;br /&gt;
| 34&lt;br /&gt;
| floating point sub&lt;br /&gt;
| Rd = Ra – Rb&lt;br /&gt;
|-&lt;br /&gt;
| fmul&lt;br /&gt;
| 35&lt;br /&gt;
| floating point multiplication&lt;br /&gt;
| Rd = Ra * Rb&lt;br /&gt;
|-&lt;br /&gt;
| fdiv&lt;br /&gt;
| 36&lt;br /&gt;
| floating point division&lt;br /&gt;
| Rd = Ra / Rb&lt;br /&gt;
|-&lt;br /&gt;
| sext8&lt;br /&gt;
| 43&lt;br /&gt;
| sign extend 8 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext16&lt;br /&gt;
| 44&lt;br /&gt;
| sign extend 16 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| sext32&lt;br /&gt;
| 45&lt;br /&gt;
| sign extend 32 bits&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| i32tof32&lt;br /&gt;
| 48&lt;br /&gt;
| cast integer to float&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| f32toi32&lt;br /&gt;
| 49&lt;br /&gt;
| cast float to integer&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| cmpeq&lt;br /&gt;
| 14&lt;br /&gt;
| compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpne&lt;br /&gt;
| 15&lt;br /&gt;
| compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpgt&lt;br /&gt;
| 16&lt;br /&gt;
| compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpge&lt;br /&gt;
| 17&lt;br /&gt;
| compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmplt&lt;br /&gt;
| 18&lt;br /&gt;
| compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmple&lt;br /&gt;
| 19&lt;br /&gt;
| compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpugt&lt;br /&gt;
| 20&lt;br /&gt;
| unsigned compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpuge&lt;br /&gt;
| 21&lt;br /&gt;
| unsigned compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpult&lt;br /&gt;
| 22&lt;br /&gt;
| unsigned compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpule&lt;br /&gt;
| 23&lt;br /&gt;
| unsigned compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfeq&lt;br /&gt;
| 37&lt;br /&gt;
| floating point compare equal&lt;br /&gt;
| Rd = Ra == Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfne&lt;br /&gt;
| 38&lt;br /&gt;
| floating point compare not equal&lt;br /&gt;
| Rd = Ra != Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfgt&lt;br /&gt;
| 39&lt;br /&gt;
| floating point compare greater then&lt;br /&gt;
| Rd = Ra &amp;gt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfge&lt;br /&gt;
| 40&lt;br /&gt;
| floating point compare greater or equal&lt;br /&gt;
| Rd = Ra &amp;gt;= Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpflt&lt;br /&gt;
| 41&lt;br /&gt;
| floating point compare less then&lt;br /&gt;
| Rd = Ra &amp;lt; Rb&lt;br /&gt;
|-&lt;br /&gt;
| cmpfle&lt;br /&gt;
| 42&lt;br /&gt;
| floating point compare less or equal&lt;br /&gt;
| Rd = Ra &amp;lt;= Rb&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== I type instructions ==&lt;br /&gt;
&lt;br /&gt;
This is the format of the I-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:I_instr.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the I-type instruction are:&lt;br /&gt;
opcode (B28-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For * I-type instructions, it is only 5 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* rs (B17-12) is the first source register.&lt;br /&gt;
* imm (B11-3) is the 9-bit immediate.&lt;br /&gt;
* fmt (B2-1) bits are used to specify if a certain operand is a scalar or a vector (one bit for every register in the format). B2 refers to register d and B1 refers to register rs. &lt;br /&gt;
&lt;br /&gt;
The I-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| ori&lt;br /&gt;
| 1&lt;br /&gt;
| or&lt;br /&gt;
| Rd = Ra | Imm&lt;br /&gt;
|-&lt;br /&gt;
| andi&lt;br /&gt;
| 2&lt;br /&gt;
| and&lt;br /&gt;
| Rd = Ra &amp;amp; Imm&lt;br /&gt;
|-&lt;br /&gt;
| xori&lt;br /&gt;
| 3&lt;br /&gt;
| xor&lt;br /&gt;
| Rd = Ra ^ Imm&lt;br /&gt;
|-&lt;br /&gt;
| addi&lt;br /&gt;
| 4&lt;br /&gt;
| addition&lt;br /&gt;
| Rd = Ra + Imm&lt;br /&gt;
|-&lt;br /&gt;
| subi&lt;br /&gt;
| 5&lt;br /&gt;
| subtraction&lt;br /&gt;
| Rd = Ra – Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulli&lt;br /&gt;
| 6&lt;br /&gt;
| multiplication&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhi&lt;br /&gt;
| 7&lt;br /&gt;
| high multiply&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| mulhui&lt;br /&gt;
| 8&lt;br /&gt;
| high multiply unsigned&lt;br /&gt;
| Rd = Ra * Imm&lt;br /&gt;
|-&lt;br /&gt;
| ashri&lt;br /&gt;
| 9&lt;br /&gt;
| arithmetic shift right&lt;br /&gt;
| Rd = Ra ‘&amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shri&lt;br /&gt;
| 10&lt;br /&gt;
| shift right&lt;br /&gt;
| Rd = Ra &amp;gt;&amp;gt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| shli&lt;br /&gt;
| 11&lt;br /&gt;
| shift left&lt;br /&gt;
| Rd = Ra &amp;lt;&amp;lt; Imm&lt;br /&gt;
|-&lt;br /&gt;
| getlane&lt;br /&gt;
| 25&lt;br /&gt;
| Get lane from vector&lt;br /&gt;
| Rd = Ra[Imm]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== MOVEI type instructions ==&lt;br /&gt;
&lt;br /&gt;
MVI (Move Immediate) has a destination register and a 16-bit instruction encoded immediate.&lt;br /&gt;
This is the format of the MOVEI-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:MOVEI_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the MOVEI-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For MOVEI-type instructions, it is only 3 bits.&lt;br /&gt;
* rd (B23-18)  is the destination register&lt;br /&gt;
* imm (B17-2) is the the 16-bit immediate.&lt;br /&gt;
* fmt (B1) is used to specify if the destination register contains a vector or a scalar.&lt;br /&gt;
&lt;br /&gt;
The MOVEI-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| moveil&lt;br /&gt;
| 0&lt;br /&gt;
| move the 16 less significant bits&lt;br /&gt;
| Rd = Ra &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| moveih&lt;br /&gt;
| 1&lt;br /&gt;
| move the 16 most significant bits&lt;br /&gt;
| Rd = (Ra &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF&lt;br /&gt;
|-&lt;br /&gt;
| movei&lt;br /&gt;
| 2&lt;br /&gt;
| move the 16 less significant bits with zero extension&lt;br /&gt;
| Rd = (Rd ^ Rd) &amp;amp; (Ra &amp;amp; 0xFFFF)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== C type instructions ==&lt;br /&gt;
This is the format of the C-type instruction encoded in machine code. &lt;br /&gt;
&lt;br /&gt;
[[File:C_format.png|800px]]&lt;br /&gt;
&lt;br /&gt;
The fields of the C-type instruction are:&lt;br /&gt;
* opcode (B26-24) is short for &amp;quot;operation code&amp;quot;. The opcode is a binary encoding for the instruction. For C-type instructions, it is only 3 bits.&lt;br /&gt;
* rs0 (B23-18) is the first source register.&lt;br /&gt;
* rs1 (B17-12) is the second source register.&lt;br /&gt;
&lt;br /&gt;
The C-type instructions are: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| barrier_core&lt;br /&gt;
| 0&lt;br /&gt;
| Memory Barrier - ensure that all explicit data memory transfers before the barrier are completed before any subsequent explicit data memory transactions starting after the barrier. Register rs0 contains the barrier identification number (BID). BID can be an arbitrary number greater than 0, i.e. BID&amp;gt;0. Different memory barriers require different BIDs. rs1 contains the number of threads that should synchronize.&lt;br /&gt;
|-&lt;br /&gt;
| flush&lt;br /&gt;
| 2&lt;br /&gt;
| Flush a cache line to the main memory. &lt;br /&gt;
|-&lt;br /&gt;
| read_cr&lt;br /&gt;
| 3&lt;br /&gt;
| Read a sub-register of the control register.&lt;br /&gt;
|-&lt;br /&gt;
| write_cr&lt;br /&gt;
| 4&lt;br /&gt;
| Write into a sub-register of the control register&lt;br /&gt;
|-&lt;br /&gt;
| dcache_inv&lt;br /&gt;
| 5&lt;br /&gt;
| Invalidates the input address line in the L1 cache.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== J type instructions ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| jmp&lt;br /&gt;
| 0&lt;br /&gt;
| jump - unconditionally jump to a specified location.&lt;br /&gt;
| PC=rd or PC=PC+offset&lt;br /&gt;
|-&lt;br /&gt;
| jmpsr&lt;br /&gt;
| 1&lt;br /&gt;
| jump to subroutine - unconditionally jump to a specified location and store the return address in the RA register.&lt;br /&gt;
| RA=PC+4 PC=rd or RA=PC+4 PC=PC+addr&lt;br /&gt;
|-&lt;br /&gt;
| jret&lt;br /&gt;
| 3&lt;br /&gt;
| Return from Subroutine - unconditionally return from a subroutine loading the return address from the RA register.&lt;br /&gt;
| PC=RA&lt;br /&gt;
|-&lt;br /&gt;
| beqz&lt;br /&gt;
| 5&lt;br /&gt;
| Conditional Branch. Branch if Equal to Zero - branche to PC+offset if the contents of the condition register is equal to zero.&lt;br /&gt;
| if(rcond==0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|-&lt;br /&gt;
| bnez&lt;br /&gt;
| 6&lt;br /&gt;
| Conditional Branch, Branch if Not Equal to Zero - branches to PC+offset if the contents of the condition register is not equal to zero.&lt;br /&gt;
| if(rcond!=0) PC=PC+offset else PC=PC+4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== M type instructions ==&lt;br /&gt;
&lt;br /&gt;
MEM (Memory Instruction) has a destination/source field, in case of load the first register asses the destination register, otherwise, in case of a store, the first register contains the store value. Next, in both cases there is the base address and the immediate. The sum of the base address and immediate will give the effective memory address.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Mnemonic&lt;br /&gt;
! Opcode&lt;br /&gt;
! Meaning&lt;br /&gt;
! Operation&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s8&lt;br /&gt;
| 0&lt;br /&gt;
| load 1 byte with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_s16&lt;br /&gt;
| 1&lt;br /&gt;
| load 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load32D&lt;br /&gt;
| 2&lt;br /&gt;
| load 1 word&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u8&lt;br /&gt;
| 4&lt;br /&gt;
| load 1 byte with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''XD''_u16&lt;br /&gt;
| 5&lt;br /&gt;
| load 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i8&lt;br /&gt;
| 7&lt;br /&gt;
| load a vector of Y bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i16&lt;br /&gt;
| 8&lt;br /&gt;
| load a vector of Y 2 bytes with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''i32&lt;br /&gt;
| 9&lt;br /&gt;
| load a vector of Y words with sign extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u8&lt;br /&gt;
| 11&lt;br /&gt;
| load a vector of Y bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u16&lt;br /&gt;
| 12&lt;br /&gt;
| load a vector of Y 2 bytes with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_v''Y''u32&lt;br /&gt;
| 13&lt;br /&gt;
| load a vector of Y words with zero extension&lt;br /&gt;
| Rd = [Rbase + Offset]&lt;br /&gt;
|-&lt;br /&gt;
| load''D''_g_32&lt;br /&gt;
| 16&lt;br /&gt;
| load 16 words from different memory addresses&lt;br /&gt;
| Rd[i] = [Rbase[i]]&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_8&lt;br /&gt;
| 32&lt;br /&gt;
| store 1 byte&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''XD''_16&lt;br /&gt;
| 33&lt;br /&gt;
| store 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store32''D''&lt;br /&gt;
| 34&lt;br /&gt;
| store 1 word&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i8&lt;br /&gt;
| 32&lt;br /&gt;
| store Y bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i16&lt;br /&gt;
| 33&lt;br /&gt;
| store Y 2 bytes&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_v''Y''i32&lt;br /&gt;
| 34&lt;br /&gt;
| store Y words&lt;br /&gt;
| [Rbase + Offset] = Rs&lt;br /&gt;
|-&lt;br /&gt;
| store''D''_s_32&lt;br /&gt;
| 42&lt;br /&gt;
| store 16 words to different memory addresses&lt;br /&gt;
| [Rbase[i]] = Rs[i]&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	<entry>
		<id>http://www.naplespu.com/doc/index.php?title=File:C_format.png&amp;diff=2234</id>
		<title>File:C format.png</title>
		<link rel="alternate" type="text/html" href="http://www.naplespu.com/doc/index.php?title=File:C_format.png&amp;diff=2234"/>
				<updated>2019-06-28T13:15:24Z</updated>
		
		<summary type="html">&lt;p&gt;Mirko: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mirko</name></author>	</entry>

	</feed>