Line 21... |
Line 21... |
*/
|
*/
|
|
|
//#include "config.h"
|
//#include "config.h"
|
//#include <math.h>
|
//#include <math.h>
|
//#include <glib->h>
|
//#include <glib->h>
|
|
//#include <iostream>
|
#include "./shared.h"
|
#include "./shared.h"
|
|
//#include "./q.hpp"
|
|
#include "pico.h"
|
//#include <stdio.h>
|
//#include <stdio.h>
|
|
|
/* Bits per threat level */
|
/* Bits per threat level */
|
#define BITS_PER_THREAT 6
|
#define BITS_PER_THREAT 6
|
|
|
|
//FIFO(x,int);
|
typedef struct {
|
//#pragma fifo_length x 24
|
int threat[2];
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_x
|
PIECE turn[2];
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_x
|
} Line;
|
//#pragma no_inter_task_stream_analysis pico_stream_input_x
|
typedef struct{
|
//#pragma no_inter_task_stream_analysis pico_stream_output_x
|
int data[MAX_CONNECT_K + 1][2];
|
/*--------------------------------------------------------------------*/
|
}threat_count_array;
|
#ifdef PICO_SYNTH
|
|
#define Q_ASSERT(_cond, _msg)
|
|
//#include <iostream>
|
|
//#include "pico.h"
|
|
//#include "q.hpp"
|
|
//#include "./shared.h"
|
|
using namespace std;
|
|
#else
|
|
/* not synthesizable */
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <assert.h>
|
|
|
|
static void debug_assert (bool cond, char * msg) {
|
|
if (!cond) {
|
|
printf("assert failed: %s\n", msg);
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
#define Q_ASSERT(_cond, _msg) debug_assert(_cond, _msg)
|
|
#endif
|
|
#define max_size 361
|
|
#define ptr_bw 32
|
|
//FIFO(queue,AIMove);
|
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_queue
|
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_queue
|
|
//#pragma no_inter_task_stream_analysis pico_stream_input_queue
|
|
//#pragma no_inter_task_stream_analysis pico_stream_output_queue
|
|
|
|
//FIFO(queue,AIMove);
|
|
//FIFO_INTERFACE(queue,AIMove);
|
|
//#pragma fifo_length pico_stream_input_queue 800
|
|
//#pragma fifo_length pico_stream_output_queue 800
|
|
//#pragma read_write_port queue separate
|
|
//#pragma bandwidth pico_stream_input_queue 1
|
|
//#pragma bandwidth pico_stream_output_queue 1
|
|
//////template <class tp=AIMove, int max_size=128, int ptr_bw=32>
|
|
////
|
|
//// /* pop front of queue, returning the front data */
|
|
//// /* q is corrupted if pop when empty */
|
|
//// AIMove q::pop (){
|
|
//// /* assert that before pop, queue is not empty (underflow check) */
|
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
|
//// "queue underflowed");
|
|
//// AIMove d = pico_stream_input_queue();
|
|
//// //cout <<"pop: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
|
//// if (head == max_size-1) {
|
|
//// head = 0;
|
|
//// wrapped = false;
|
|
//// } else {
|
|
//// head = head + 1;
|
|
//// }
|
|
//// return d;
|
|
//// }
|
|
////
|
|
//// /* push data into back of queue */
|
|
//// /* q is corrupted if push when full */
|
|
//// void q::push (AIMove d){
|
|
//// pico_stream_output_queue(d);
|
|
//// if (tail == max_size-1) {
|
|
//// tail = 0;
|
|
//// wrapped = true;
|
|
//// } else {
|
|
//// tail = tail + 1;
|
|
//// }
|
|
//// /* assert that after push, queue is not empty (overflow check) */
|
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
|
//// "Queue overflowed") ;
|
|
//// //cout <<"push: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
|
//// }
|
|
////
|
|
//// /* return current size of the queue */
|
|
//// int q::size (){
|
|
//// if (wrapped) {
|
|
//// return (max_size - head) + (tail - 0);
|
|
//// } else {
|
|
//// return tail - head;
|
|
//// }
|
|
//// }
|
|
/////*--------------------------------------------------------------------*/
|
|
////void test(int ready){
|
|
//// int i;
|
|
//// //for (i=0;i<10;i++) if(ready>1) pico_stream_input_x();
|
|
//// //for (i=0;i<10;i++) {if(!moves_fifo1.empty()) moves_fifo1.pop(); }
|
|
//// AIMove m;
|
|
//// while(1) {
|
|
//// if(ready>1) m=pico_stream_input_queue();
|
|
//// if(m.weight==-1) break;
|
|
//// }
|
|
////}
|
|
////q moves_fifo1;
|
|
|
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
|
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
|
/* Bit pack the threat value */
|
/* Bit pack the threat value */
|
{
|
{
|
if (threat < 1)
|
if (threat < 1)
|
Line 95... |
Line 190... |
p = piece_at(b, x, y);
|
p = piece_at(b, x, y);
|
if (!piece_empty(p))
|
if (!piece_empty(p))
|
return 0;
|
return 0;
|
|
|
/* Push forward the maximum and find the window type */
|
/* Push forward the maximum and find the window type */
|
#pragma unroll
|
//#pragma unroll
|
|
//#pragma num_iterations(1,3,6)
|
for (maximum = 1; maximum < connect_k; maximum++) {
|
for (maximum = 1; maximum < connect_k; maximum++) {
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
if (p == PIECE_ERROR)
|
if (p == PIECE_ERROR)
|
break;
|
break;
|
if (!piece_empty(p)) {
|
if (!piece_empty(p)) {
|
Line 111... |
Line 207... |
}
|
}
|
}
|
}
|
maximum--;
|
maximum--;
|
|
|
/* Try to push the entire window back */
|
/* Try to push the entire window back */
|
#pragma unroll
|
//#pragma unroll
|
|
//#pragma num_iterations(1,3,6)
|
for (minimum = -1; minimum > -connect_k; minimum--) {
|
for (minimum = -1; minimum > -connect_k; minimum--) {
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
if (p == PIECE_ERROR || piece_empty(p))
|
if (p == PIECE_ERROR || piece_empty(p))
|
break;
|
break;
|
if (type == PIECE_NONE)
|
if (type == PIECE_NONE)
|
Line 133... |
Line 230... |
minimum++;
|
minimum++;
|
|
|
/* Push back minimum if we haven't formed a complete window, this window
|
/* Push back minimum if we haven't formed a complete window, this window
|
can't be a double */
|
can't be a double */
|
if (maximum - minimum < connect_k - 1) {
|
if (maximum - minimum < connect_k - 1) {
|
|
//#pragma unroll
|
|
//#pragma num_iterations(1,3,6)
|
for (minimum--; minimum > maximum - connect_k; minimum--) {
|
for (minimum--; minimum > maximum - connect_k; minimum--) {
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
if (p == PIECE_ERROR)
|
if (p == PIECE_ERROR)
|
break;
|
break;
|
if (!piece_empty(p)) {
|
if (!piece_empty(p)) {
|
Line 155... |
Line 254... |
if (maximum - minimum >= connect_k - 1)
|
if (maximum - minimum >= connect_k - 1)
|
return count;
|
return count;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,AIMoves *moves,int k)
|
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound)
|
{
|
{
|
|
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma internal_blockram threat_counts
|
//#pragma internal_blockram threat_counts
|
//#pragma no_memory_analysis threat_counts
|
//#pragma no_memory_analysis threat_counts
|
|
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram b
|
|
#pragma internal_blockram bwrite
|
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/* This is the line of threats currently being processed */
|
/* This is the line of threats currently being processed */
|
Line line[board_size]={{1},{2}};
|
Line line[board_size]={{1},{2}};
|
#pragma internal_fast line
|
#pragma internal_fast line
|
Line 243... |
Line 340... |
y -= dy;
|
y -= dy;
|
}
|
}
|
return weight;
|
return weight;
|
}
|
}
|
|
|
|
FIFO(queue,AIMove);
|
|
//FIFO_INTERFACE(queue,AIMove);
|
|
#pragma fifo_length pico_stream_input_queue 800
|
|
#pragma fifo_length pico_stream_output_queue 800
|
|
#pragma bandwidth pico_stream_input_queue 1
|
|
#pragma bandwidth pico_stream_output_queue 1
|
/*AIMoves*/int ai_threats(Board *board,AIMove *move)
|
/*AIMoves*/int ai_threats(Board *board,AIMove *move)
|
{
|
{
|
//#pragma read_write_ports board.data combined 2
|
//#pragma read_write_ports board.data combined 2
|
#pragma internal_blockram board
|
//#pragma internal_blockram board
|
//#pragma no_memory_analysis board
|
//#pragma no_memory_analysis board
|
|
|
//#pragma internal_blockram move
|
//#pragma internal_blockram move
|
//#pragma no_memory_analysis move
|
//#pragma no_memory_analysis move
|
|
|
/////////* All threat functions work on this board */
|
/////////* All threat functions work on this board */
|
/*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
/*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram b
|
#pragma internal_blockram b
|
|
//#pragma multi_buffer b 2
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
/*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram bwrite
|
#pragma internal_blockram bwrite
|
|
//#pragma multi_buffer bwrite 2
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
|
/*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma read_write_ports moves.data combined 3
|
#pragma internal_blockram moves
|
#pragma internal_blockram moves
|
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
|
|
moves.len=0;
|
moves.len=0;
|
Line 304... |
Line 409... |
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// u_sum += threat_line(board_size - 1, i, -1, 1,&b);
|
// u_sum += threat_line(board_size - 1, i, -1, 1,&b);
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
//rewritten for hardware
|
//rewritten for hardware
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
|
//int id= PICO_initialize_PPA(threat_line);
|
|
//PICO_set_task_overlap(id, 2);
|
int j;
|
int j;
|
int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
|
int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
|
int k=0;
|
int k=0;
|
for(j=0;j<6;j++){
|
for(j=0;j<6;j++){
|
switch(j){
|
switch(j){
|
Line 406... |
Line 513... |
break;
|
break;
|
}
|
}
|
}
|
}
|
|
|
|
|
u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,&moves,k);
|
u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
|
}
|
}
|
}
|
}
|
|
//PICO_sync_task(id, 1);
|
|
//PICO_finalize_PPA(id);
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
//board_copy(&b,&b_marks);
|
//board_copy(&b,&b_marks);
|
|
unsigned int index[max_size]={0};
|
|
#pragma bitsize index 9
|
|
#pragma internal_fast index
|
|
AIMoves moves1;
|
|
#pragma internal_blockram moves1
|
/*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves);
|
/*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves);
|
moves.utility = u_sum;
|
//test(ready);
|
if (!aimoves_choose(&moves, move))
|
streamsort(&moves1,&index[0]);
|
|
//moves1.utility = u_sum;
|
|
//moves.utility = u_sum;
|
|
/*----------------------------
|
|
rewritten for hardware
|
|
----------------------------*/
|
|
//if (!aimoves_choose(&moves1, move))
|
|
// return 0;
|
|
//else return 1;
|
|
int ret_val;
|
|
ret_val=aimoves_choose(&moves1, move,&index[0]);
|
|
if (!ret_val)
|
return 0;
|
return 0;
|
else return 1;
|
else return 1;
|
|
/*----------------------------
|
|
end rewritten for hardware
|
|
----------------------------*/
|
//board_free(b);
|
//board_free(b);
|
//return moves;
|
//return moves;
|
//return 0;
|
//return 0;
|
}
|
}
|
|
|
Line 514... |
Line 642... |
//AIMoves *moves = aimoves_new();
|
//AIMoves *moves = aimoves_new();
|
//AIMoves moves;
|
//AIMoves moves;
|
//AIMoves moves[361];
|
//AIMoves moves[361];
|
AIMove move;
|
AIMove move;
|
PIECE p;
|
PIECE p;
|
|
//moves_fifo.resoet();
|
|
AIMove m;
|
|
#pragma num_iterations(19,19,19)
|
for (move.y = 0; move.y < board_size; move.y++)
|
for (move.y = 0; move.y < board_size; move.y++)
|
for (move.x = 0; move.x < board_size; move.x++)
|
#pragma num_iterations(19,19,19)
|
|
for (move.x = 0; move.x < board_size; move.x++){
|
if ((p = piece_at(b, move.x, move.y)) >= minimum) {
|
if ((p = piece_at(b, move.x, move.y)) >= minimum) {
|
move.weight = p - PIECE_THREAT0;
|
move.weight = p - PIECE_THREAT0;
|
aimoves_set(moves, &move);
|
aimoves_set(moves, &move);
|
|
pico_stream_output_queue(move);
|
|
//cout<<"push"<<move.weight<<endl;
|
|
//ready=ready+1;
|
|
//cout<<"READY"<<*ready<<endl;
|
|
}else {
|
|
m.weight =-100;
|
|
pico_stream_output_queue(m);
|
|
//cout<<"push"<<m.weight<<endl;
|
|
}
|
|
//if((move.y == board_size-1) && (move.x == board_size-1)){
|
|
// m.weight=-1;m.x=-1;m.y=-1;
|
|
// /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
|
|
//
|
|
//}
|
}
|
}
|
|
//moves_fifo.active=0;
|
//return moves;
|
//return moves;
|
}
|
int i;
|
|
//for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
|
|
//for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
|
|
//for (i=0;i<10;i++) {
|
|
// *ready=i;pico_stream_output_queue(m);
|
|
// if(i==9){
|
|
// m.weight=-1;
|
|
// pico_stream_output_queue(m);
|
|
// }
|
|
//
|
|
// }
|
|
|
|
}
|
|
void streamsort(AIMoves *moves,unsigned int *index){
|
|
/* Insertion sort for streaming*/
|
|
AIMove val;
|
|
AIMove data[361]={{-1},{-1},{-1}};
|
|
//unsigned int index[361];
|
|
#pragma bitsize index 9
|
|
#pragma internal_fast index
|
|
int i,j,k;
|
|
unsigned int len=0;
|
|
moves->data[0].weight=-100;
|
|
// while(1) {
|
|
// if(!moves_fifo.empty()){
|
|
// val=moves_fifo.pop();
|
|
// for(i=0;i<len;i++){
|
|
// if (list[i].weight < val.weight){
|
|
// for(j=len-1;j>i-1;j--){
|
|
// list[j+1]=list[j];
|
|
// }
|
|
// break;
|
|
// }
|
|
// }
|
|
// list[i]=val;
|
|
// len++;
|
|
// }
|
|
// else break;
|
|
// //if(!moves_fifo.active && moves_fifo.empty()) break;
|
|
// }
|
|
//while(1) {
|
|
//int count=0;
|
|
#pragma num_iterations(1,150,1362)
|
|
for(k=0;k<1362;k++){
|
|
//count++;
|
|
//cout<<count<<endl;
|
|
if (k>1000){
|
|
|
|
//if(ready>5){
|
|
val=pico_stream_input_queue();
|
|
//cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
|
|
if(val.weight==-1) {moves->len=len;break;}
|
|
else if(val.weight==-100) continue;
|
|
#pragma num_iterations(0,150,361)
|
|
for(i=0;i<len;i++){
|
|
if (moves->data[index[i]].weight < val.weight){
|
|
for(j=len-1;j>i-1;j--){
|
|
//moves->data[j+1]=moves->data[j];
|
|
index[j+1]=index[j];
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
index[i]=len;
|
|
moves->data[len]=val;
|
|
len++;
|
|
//cout<<"STREAMSORT"<<":";
|
|
//}
|
|
/*else*/ //{moves->len=len;break;}
|
|
}
|
|
}
|
|
moves->len=len;
|
|
//cout<<"STREAMSORT"<<":"<<moves->len<<endl;
|
|
//for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
|
|
//cout<<endl;
|
|
//for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
|
|
//cout<<endl;
|
|
}
|
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
|
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
|
{
|
{
|
int dx, dy, count;
|
int dx, dy, count;
|
#pragma bitsize dx 4
|
#pragma bitsize dx 4
|
#pragma bitsize dy 4
|
#pragma bitsize dy 4
|
Line 549... |
Line 772... |
//AIMoves *moves;
|
//AIMoves *moves;
|
AIMove move;
|
AIMove move;
|
|
|
move.weight = AIW_NONE;
|
move.weight = AIW_NONE;
|
//moves = aimoves_new();
|
//moves = aimoves_new();
|
for (move.y = 0; move.y < board_size; move.y++)
|
#pragma num_iterations(1,9,19)
|
for (move.x = 0; move.x < board_size; move.x++)
|
for (move.y = 0; move.y < board_size; move.y++){
|
|
#pragma num_iterations(1,9,19)
|
|
for (move.x = 0; move.x < board_size; move.x++){
|
if (is_adjacent(b, move.x, move.y, dist))
|
if (is_adjacent(b, move.x, move.y, dist))
|
aimoves_append(moves, &move);
|
aimoves_append(moves, &move);
|
|
}
|
|
}
|
//aimoves_shuffle(moves,current_random);
|
//aimoves_shuffle(moves,current_random);
|
//return moves;
|
//return moves;
|
}
|
}
|
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
|
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
|
{
|
{
|
Line 570... |
Line 797... |
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma internal_blockram moves
|
//#pragma internal_blockram moves
|
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
//AIMove move;
|
//AIMove move;
|
|
unsigned int index[1]={0};
|
//AIMoves *moves;
|
//AIMoves *moves;
|
moves.len=0;
|
moves.len=0;
|
/* Get all open tiles adjacent to any piece */
|
/* Get all open tiles adjacent to any piece */
|
/*moves =*/ enum_adjacent(b, 1,&moves,current_random);
|
/*moves =*/ enum_adjacent(b, 1,&moves,current_random);
|
if (moves.len){
|
if (moves.len){
|
aimoves_choose(&moves, move);
|
aimoves_choose(&moves, move,&index[0]);
|
|
|
return ;//moves;
|
return ;//moves;
|
}
|
}
|
/* Play in the middle if there are no open adjacent tiles */
|
/* Play in the middle if there are no open adjacent tiles */
|
move->x = board_size / 2;
|
move->x = board_size / 2;
|