# XSCT 命令参考 (SDK 2018.3)

> 适用版本: Vivado SDK ≤ 2019.2
> XSCT = Xilinx Software Command-line Tool

## 工作空间管理

```tcl
# 设置工作空间
setws {C:/project/sdk_workspace}

# 获取当前工作空间
getws
```

## 硬件平台

```tcl
# 从 .sysdef/.hdf 创建硬件工程
createhw -name hw_platform -hwspec {path/to/system.sysdef}

# 列出处理器
getprocessors hw_platform
# 返回: ps7_cortexa9_0 ps7_cortexa9_1
```

## BSP (Board Support Package)

```tcl
# 创建 BSP
createbsp -name bsp_standalone \
    -hwproject hw_platform \
    -proc ps7_cortexa9_0 \
    -os standalone

# 自定义 BSP 参数
configbsp -bsp bsp_standalone stdin ps7_uart_1
configbsp -bsp bsp_standalone stdout ps7_uart_1
configbsp -bsp bsp_standalone extra_compiler_flags {-O2 -DNDEBUG}

# 添加库
setlib -bsp bsp_standalone -lib lwip211
setlib -bsp bsp_standalone -lib xilffs

# 重新生成 BSP
updatemss -mss {bsp_standalone/system.mss}
regenbsp -bsp bsp_standalone
```

## 应用工程

```tcl
# 创建空应用
createapp -name my_app \
    -hwproject hw_platform \
    -bsp bsp_standalone \
    -proc ps7_cortexa9_0 \
    -lang c

# 从模板创建
createapp -name hello_world \
    -hwproject hw_platform \
    -bsp bsp_standalone \
    -proc ps7_cortexa9_0 \
    -lang c \
    -app {Hello World}

# 模板列表
getapptemps
# Empty Application, Hello World, Memory Tests, Zynq FSBL, etc.
```

## 编译

```tcl
# 编译单个工程
projects -build -name my_app

# 编译全部
projects -build

# 清理
projects -clean -name my_app
```

## 调试：连接与下载

```tcl
# 连接到目标板
connect

# 列出目标
targets
# 返回:
#   1  APU
#   2  ARM Cortex-A9 MPCore #0
#   3  ARM Cortex-A9 MPCore #1
#   4  xc7z020

# 选择目标
targets 2

# 复位处理器
rst -processor

# 下载 FPGA 比特流
fpga {path/to/system.bit}

# 下载 ELF
dow {path/to/my_app.elf}

# 运行
con

# 暂停
stop

# 断开
disconnect
```

## JTAG 操作

```tcl
# 读内存
mrd 0x40000000 10

# 写内存
mwr 0x40000000 0xDEADBEEF

# 读寄存器
rrd
```

## 完整开发流程

```tcl
# 1. 设置工作空间
setws {C:/project/sdk_workspace}

# 2. 创建硬件
createhw -name hw -hwspec {../vivado/export/system.sysdef}

# 3. 创建 BSP
createbsp -name bsp -hwproject hw -proc ps7_cortexa9_0 -os standalone

# 4. 配置 BSP
configbsp -bsp bsp stdin ps7_uart_1
configbsp -bsp bsp stdout ps7_uart_1
regenbsp -bsp bsp

# 5. 创建应用
createapp -name app -hwproject hw -bsp bsp -proc ps7_cortexa9_0 -lang c

# 6. 编译
projects -build

# 7. 连接并下载
connect
targets 2
rst -processor
fpga {hw/system.bit}
dow {app/Debug/app.elf}
con
```
